SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 split one row with date into many
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

birtprofi
Starting Member

Austria
6 Posts

Posted - 07/04/2013 :  10:30:30  Show Profile  Reply with Quote
Hi guys, I need your help.

I have a table that contains vacation data from | til. for example:

ID | Name | From | Til
1 | xy | 2013-07-01 | 2013-07-20 |
2 | ab | 2013-06-15 | 2013-06-25 |

For a reporting tool I need for every "ID" a single row. Like this:

ID | Name | Date
1 | xy | 2013-07-01
1 | xy | 2013-07-02
1 | xy | 2013-07-03
....

So please give me your advices. Is this possible with sql?

best regards
rf

rf

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/04/2013 :  11:19:57  Show Profile  Reply with Quote



DECLARE @Temp TABLE (ID INT, NAME VARCHAR(10), [From] DATE, [Til] DATE);

INSERT INTO @Temp VALUES
(1, 'xy', '2013-07-01', '2013-07-20'),
(2, 'ab', '2013-06-15', '2013-06-25');


SELECT T.ID, T.Name, DATEADD(dd, number, [From]) as [DATE] 
      FROM @Temp T, master..spt_values where type = 'P' and 
        number <= DATEDIFF(dd, [From], [Til]);


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 07/04/2013 :  12:24:33  Show Profile  Reply with Quote
If you've a calendar table you can utilize that too instead of spt_values
I generally dont prefer using system objects like spt_value especially in production. even if no calendar table is present, I usually create a tally table for this purpose as in Jeffs article

http://www.sqlservercentral.com/articles/T-SQL/62867/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3326 Posts

Posted - 07/04/2013 :  12:36:47  Show Profile  Reply with Quote
If you don't have permissions to create tables in the database, another option is to use a CTE to create a tally table on the fly: http://blogs.inkeysolutions.com/2011/05/creating-tally-tables-using-cte-in-sql.html
Go to Top of Page

birtprofi
Starting Member

Austria
6 Posts

Posted - 07/08/2013 :  03:33:03  Show Profile  Reply with Quote
HI guy´s

thank you very much for your help. Your suggestions and solutions works perfect for me.

best regards
rafael

rf
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 07/08/2013 :  03:34:30  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000