Click here to Skip to main content
15,799,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have created a table variable that creates 24 hours but instead of table variable i want to create CTE that returns 24 hours so please help how to get 24 hours from with clause. Here my requirement is comparing hourswise orders shipped between table variable and source table if on a particular hour like 10am on no orders shipped then result should be 0 this is return from table variable for this i am using left join. Here my requirement is instead of table variable how to create CTE. Here i am giving some sample query that returns 24 hours.
SQL
Declare @Hours Table (Hour int);
Declare @i as int;
Set @i = 5
While @i < 24
Begin
   Insert into @Hours
   Values(@i);

   Set @i = @i + 1;
End;
Posted
Updated 2-Oct-12 21:40pm
v2

1 solution

Hi Madhu ,

Try this code block

SQL
 WITH hourscte AS(
    SELECT 5 Hour
      UNION ALL
    SELECT Hour + 1
     FROM hourscte
     WHERE Hour  < 24
 )

SELECT * FROM hourscte
OPTION (MAXRECURSION 0)


If you want to display dates in a range, then go through the link

http://www.mindfiresolutions.com/Display-Dates-In-A-Range-1891.php[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900