Welcome to EMC Consulting Blogs Sign in | Join | Help

Jim 2.0

T-SQL Calendar table

After experimentation with various datepart and datename methods following my most recent blog post, I was able to identify many if not all the necessary components for constructing a calendar table.To do this I have combined these functions with the SQL to generate a list of dates as posted by Jamie some time ago. The resultant SQL is given below, and hopefully does not require any specific explanation, however feel free to leave me a comment if you would like clarification on any part of it.

with    mycte as

        (

        select cast('2007-04-16' as datetime) DateValue

        union all

        select DateValue + 1

        from    mycte   

        where   DateValue + 1 < '2007-04-23'

        )

 

select  DateValue

,       datepart(dy, DateValue) [day of year]

,       datename(dw, DateValue) [day]

,       datepart(dw, DateValue-1) [day of week]

,       datepart(dd, DateValue) [day of month]

,       datepart(ww, DateValue) [week]

,       datepart(mm, DateValue) [month]

,       datename(mm, DateValue) [month]

,       datepart(qq, DateValue) [quarter]

,       datepart(yy, DateValue) [year]

,       datepart(HH, DateValue) [HOUR]

,       datepart(MI, DateValue) [MIN]

,       datepart(SS, DateValue) [SEC]

,       datepart(MS, DateValue) [MILLISECOND]

from    mycte

OPTION  (MAXRECURSION 0)

James

Published 24 April 2007 15:51 by James.Pipe

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Jim 2.0 said:

In addition to the T-SQL calendar table , which I have posted seperately, I went the whole hog and figured

April 24, 2007 16:17
 

Roy said:

How would you perform this query on SQL 2000?

May 19, 2008 16:41
 

Roy said:

Got is to work using #temp table. Hope this helps someone as your great post helped me!

Thanks,

Roy

-------------------------------

declare @start datetime,

@end datetime

set @start = '1900-01-01'

set @end = '2200-01-01'

declare @no_of_Days int

set @no_of_days = datediff(dd,@start,@end) + 1

set rowcount @no_of_days

select identity(int,0,1) as dy into #temp from sysobjects a, sysobjects b

set rowcount 0

select dateadd(dd,dy,@start) as [days]

,       datepart(dy, dateadd(dd,dy,@start)) [day of year]

,       datename(dw, dateadd(dd,dy,@start)) [day]

,       datepart(dw, dateadd(dd,dy,@start)-1) [day of week]

,       datepart(dd, dateadd(dd,dy,@start)) [day of month]

,       datepart(ww, dateadd(dd,dy,@start)) [week]

,       datepart(mm, dateadd(dd,dy,@start)) [month]

,       datename(mm, dateadd(dd,dy,@start)) [month]

,       datepart(qq, dateadd(dd,dy,@start)) [quarter]

,       datepart(yy, dateadd(dd,dy,@start)) [year]

from #temp

drop table #temp

May 19, 2008 17:09
 

M said:

Very Neat! Worked 110%

October 13, 2008 16:44
 

Juris said:

Good! Exact what I was looking for. Thanks for post!

October 15, 2008 11:47
 

Nona said:

OH WOW...SO freaking COOL!

February 24, 2009 19:46
 

Curtis Bragdon said:

Excellent, this is just what I was looking for - thanks

May 23, 2009 15:23
 

Ulises said:

Its a pity that this doesn't work inside a Inline UDF because the OPTION keyword is not accepted there. You would need to use the OPTION outside of the UDF or you would be limited to the default MAXRECURSION value of 100.

June 17, 2009 18:16
 

Sadhan Damal said:

Excellent, Nice logic!!! it's need full 4 every one who need calendar in sql!

thanks for posting!!! hv nice time ahead!

December 13, 2009 06:46
 

Luca said:

Good work!

June 25, 2010 12:13
 

Viral said:

It has worked as per my expectations..... great!!!!

August 26, 2010 14:01
 

Rachel said:

That's awesome. Thank you!

February 20, 2012 18:43

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems