| Author |
Topic |
|
Philip.Fernandez
Starting Member
4 Posts |
Posted - 2010-01-20 : 13:16:03
|
Hello. This is my first post here, and I am relatively new to T-SQL, so please go easy on me. This isn't a homework question. This is something I'm trying to do to help me at work.I'd like to create a temporary table with a date and hour column, and fill the dates and hours using some kind of a WHILE loop. For example:CREATE TABLE #request ( date DATETIME NOT NULL, hour INT NOT NULL ) I then want to create a WHILE loop to INSERT into the dates and hour columns 24 hours per day for a specific date range. For example, if I set the first day in the WHILE loop to 1/1/2005 and the end date in the WHILE loop to 1/31/2006, I'd like to see... date hour---------- ---- 1/1/2005 1 1/1/2005 2 1/1/2005 3... 1/1/2005 24... 1/31/2006 24 Can anyone please provide me with suggestions on how to go about doing this? Thank you in advance. |
|
|
shan
Yak Posting Veteran
84 Posts |
Posted - 2010-01-20 : 14:03:39
|
| See the below scriptCREATE TABLE #request ( date DATETIME NOT NULL, hour INT NOT NULL ) declare @start_dt datetimedeclare @end_dt datetimedeclare @i intset @start_dt = '12/31/2009'set @end_dt = '01/05/2010'set @i=0while (@start_dt <= @end_dt)begin WHILE (@i<25) BEGIN insert #request select @start_dt,@i set @i = @i +1 ENDset @start_dt = @start_dt + 1END-Shan |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-01-20 : 14:23:45
|
| You can even do it without a while loopJimselect dateadd(hour,a.number,dateadd(day,spt.number,'01/01/2005'))frommaster..spt_values spt CROSS APPLY(select spt.number frommaster..spt_values sptwhere type = 'p' and spt.number < 24) a where type = 'p'and spt.number < 396order by 1Everyday I learn something that somebody else already knew |
 |
|
|
Philip.Fernandez
Starting Member
4 Posts |
Posted - 2010-01-20 : 14:24:33
|
| Jim:Thanks for your suggestion. I'm not familiar with the CROSS APPLY functions and their syntax, so it'll take some time for me to dissect how that's working.Shan:I tried your script with the modification of setting @i to 1 instead of 0. However, I noticed that this only updates the table for one day's worth of hours. It's as though the start date is not incrementing, even with the "set @start_dt = @start_dt + 1" function. Do you know what might be causing the loop to not continue to the next day? |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-01-20 : 14:37:01
|
| CROSS APPLY (as well as OUTER APPLY) are new in SQL Serve 2005. Jimselect dateadd(day,spt.number,'01/01/2005'),a.numberfrommaster..spt_values sptCROSS JOIN(select spt.number frommaster..spt_values sptwhere type = 'p' and spt.number between 1 and 24) awhere type = 'p'and spt.number < 396order by 1Everyday I learn something that somebody else already knew |
 |
|
|
shan
Yak Posting Veteran
84 Posts |
Posted - 2010-01-20 : 15:06:46
|
| This will work..CREATE TABLE #request ( date DATETIME NOT NULL, hour INT NOT NULL ) declare @start_dt datetimedeclare @end_dt datetimedeclare @i intdeclare @t intset @start_dt = '12/31/2009'set @end_dt = '01/05/2010'--set @t = set @i=0while (DATEDIFF(dd, @start_dt, @end_dt) <> 0)begin set @i = 0 WHILE (@i<24) BEGIN set @i = @i +1 insert #request select @start_dt,@i END set @start_dt = DATEADD("day", 1,@start_dt)ENDselect * from #request-Shan |
 |
|
|
Philip.Fernandez
Starting Member
4 Posts |
Posted - 2010-01-20 : 15:20:36
|
| Shan:I think I've found the problem and corrected it. I simply didn't reset @i at any point, so it hit 24 and and stopped after the first day. Thank you for your second script though. I will try it and see if it is a better solution than your first script with my minor adjustments.Thanks again for your help. It's very much appreciated. |
 |
|
|
shan
Yak Posting Veteran
84 Posts |
Posted - 2010-01-20 : 15:35:26
|
You are welcome -Shan |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|