Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert single record with a range into multiple records
Message
 
 
To
07/01/2010 16:11:47
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01442682
Message ID:
01442687
Views:
31
This message has been marked as a message which has helped to the initial question of the thread.
I don't think it's the best query, but simple enough
-- Test query
declare @T table (cName nChar(50), nStartYear numeric(4,0), nEndYear Numeric(4,0))
insert into @T select 'Name1', 2000, 2003
union all
 select 'Name1', 2010, 2012
union all
 select 'Name1', 2010, 2012

-->cName                  nStartYear                   nEndYear
-->Name1                  2000                            2003
-->Name2                  2010                            2012
-->Name3                  2010                            2010

--select Number from master.dbo.spt_values where type = 'P'

select T.cName, T.nStartYear + X.Number as nYear from @T T
inner join (select Number from master.dbo.spt_values where type = 'P') X 
on T.nStartYear + X.number <= T.nEndYear order by T.cName, nYear
>Hi All,
>I need help construct SQL Statement in T-SQL, which will convert single record with a range into multiple records. (MS SQL Server 2005)
>
>The source table has structure like:
>
>cName nChar(50)
>nStartYear numeric(4,0)
>nEndYear Numeric(4,0)
>
>cName nStartYear nEndYear
>Name1 2000 2003
>Name2 2010 2012
>Name3 2010 2010
>
>The resulting set should look like
>cName nYear
>Name1 2000
>Name1 2001
>Name1 2002
>Name1 2003
>Name2 2010
>Name2 2011
>Name2 2012
>Name3 2010
>
>Thank you in advance
>
>Yelena
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform