In SQL Server 2000, I want to be able to list all dates between 2 dates including hours for each day so if I have @fromdate = 2016-07-01 00:00:00
and @todate = 2016-07-05 00:00:00
then I need a list of:
2016-07-01 00:00:00
2016-07-01 01:00:00
2016-07-01 02:00:00
2016-07-01 03:00:00
2016-07-01 04:00:00
...
2016-07-04 23:00:00
2016-07-05 00:00:00
Now this query works half way through:
declare @fromdate as datetime
declare @todate as datetime
set @FromDate = '2016-07-11 00:00:00.000'
set @ToDate = '2016-07-21 00:00:00.000'
declare @i int, @j int
set @i = convert(int, @todate - @fromdate)
set @j = 0
while @i >= 0
begin
while @j < 24
begin
select dateadd(hour, @j, @ToDate - @i) as date, @j as Hour
set @j = @j + 1
end
set @i = @i - 1
end
It returns an hourly split for the first day but doesn't jump to the next day. If I disable the inner while
loop the outer loop works fine without hours. Any suggestions are welcome, remember it's a SQL Server 2000 so nothing advanced will work.