| Author |
Topic |
|
caisys
Starting Member
16 Posts |
Posted - 2007-12-19 : 08:02:35
|
| Hi,I have a vacations table like this:id,startday,endday,description-------------------------------1,2008-01-01,2008-01-03,New Year Vacation2,2008-01-15,2008-01-16,Mid Month Vacation3,2008-01-25,2008-01-25,Another vacationI want to run a query to select vacations in Jan 08 that would returnVacationDay,Description-----------------------2008-01-01,New Year Vacation2008-01-02,New Year Vacation2008-01-03,New Year Vacation2008-01-15,Mid Month Vacation2008-01-16,Mid Month Vacation2008-01-25,Another VacationI know the F_TABLE_DATE function can fill in the dates for each vacation record but how can i repeat the function for each record passing the startdate and end date then union all in one result set?Thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 08:09:25
|
[code]-- Prepare sample dataDECLARE @Sample TABLE (ID INT, StartDay DATETIME, EndDay DATETIME, Description VARCHAR(100))INSERT @SampleSELECT 1, '2008-01-01', '2008-01-03', 'New Year Vacation' UNION ALLSELECT 2, '2008-01-15', '2008-01-16', 'Mid Month Vacation' UNION ALLSELECT 3, '2008-01-25', '2008-01-25', 'Another vacation'-- Show the expected outputSELECT f.Date, p.DescriptionFROM @Sample AS pCROSS APPLY F_TABLE_DATE(p.StartDay, p.EndDay) AS f[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2007-12-20 : 01:03:09
|
| ;with cte(VaccationDay,EndDay,Description)as( select StartDay,EndDay,Description from @Sample where datepart(mm,StartDay)=12 and datepart(yyyy,StartDay)=2008union allselect VaccationDay+1,EndDay,Description from cte c where VaccationDay < EndDay )select VaccationDay,Description from cte where datepart(mm,VaccationDay)=12 and datepart(yyyy,VaccationDay)=2008order by VaccationDay |
 |
|
|
|
|
|