Anatoly Lubarsky Logo
programming, design, integration, games, music

Generate Calendar Dates. Version 2.0

Concerning my howto on sqljunkies I've got a feedback from Thomas Darmstandler. Thanks, Tom. He points out some things I didn't make up to the end. These are:


1) not returning 365 or 366 days for the year
2) although my version #1 takes care of a leap year, 
you still need to figure out whether to return 365 or 366 days

The following function by Tom returns the days of the year you requested.


alter function fn_CalendarTbl
/************************************************
Function: fn_CalendarTbl
Purpose: Creates an on the fly table for date 
  searches and joins 
Returns: A table of one year of dates
Example Call: select * from fn_CalendarTbl(2003)
  select * from fn_CalendarTbl(2004)  
Notes:  Accounts for leap years  
Authors: Anatoly Lubarsky / Thomas Darmstandler
--------        ---------      ----     ----------
History: 4/8/2004  Created
************************************************/

(@yr int)
returns @t2 table (dt datetime)

as
begin
declare @startDt datetime,
 @dt datetime,
 @leapDt datetime,
 @retVal int,
 @enterDt varchar(20)

declare @t table (dt smalldatetime)

set @startDt = convert(smalldatetime, 
               convert(varchar(4), @yr) + '0101')

insert into @t
select dt
from
 (select
 @startDt +
 n3.num * 100 +
 n2.num * 10 +
 n1.num as dt
 from
  (select 0 as num union all
  select 1 union all
  select 2 union all
  select 3 union all
  select 4 union all
  select 5 union all
  select 6 union all
  select 7 union all
  select 8 union all
  select 9
  ) n1,
   (select 0 as num union all
   select 1 union all
   select 2 union all
   select 3 union all
   select 4 union all
   select 5 union all
   select 6 union all
   select 7 union all
   select 8 union all
   select 9) n2,
    (select 0 as num union all
    select 1 union all
    select 2 union all
    select 3) n3) gencalendar
order by 1

---------------
-- accounts for leap years
set @enterDt ='2/28/' + cast(@yr as varchar(4))
set @leapDt = (select dateadd(d,1,@enterDt))
set @retVal = (select datepart(d,@leapDt))

if @retVal = 29
  begin
 insert into @t2
 select top 366 * 
 from @t
  end 
else
  begin

 insert into @t2
 select top 365 * 
 from @t
  end 

return
end 


My response: It works fine, but the perfomance is not good because:


1) performing the whole insert twice
2) extra variables for example: extra table variable, 
extra datetime variable. In udf you have to be aware of it.
3) datetime conversion formats won't work under certain locals 


So I enhanced my version #1 a bit. So you get version #2:


/*
########################################################
#
#   F_TBL_CALENDAR
#
#   version #2
#
#   use: select * from dbo.F_TBL_CALENDAR(2003)
#
########################################################
*/

alter function F_TBL_CALENDAR
(@p_year smallint)
returns @tbl table (cal_date smalldatetime)
as
begin
 
   -- 1 --
   -- check for leap year   
   declare @p_leap_date smalldatetime
   declare @p_check_day int
 
   set @p_leap_date = cast(@p_year as varchar(4)) + '0228'
   set @p_leap_date = (select dateadd(d, 1, @p_leap_date))
   set @p_check_day = (select datepart(d, @p_leap_date))

   -- 2 --
   -- create start date
   declare @p_start_date smalldatetime
   set @p_start_date = convert(smalldatetime, 
                       convert(varchar(4), @p_year)
                       + '0101')
   -- 3 --
   -- generate dates
   insert into @tbl
   select top 365 cal_date
   from
   (
      select
         @p_start_date +
         n3.num * 100 +
         n2.num * 10 +
         n1.num as cal_date
      from
      (
         select 0 as num union all
         select 1 union all
         select 2 union all
         select 3 union all
         select 4 union all
         select 5 union all
         select 6 union all
         select 7 union all
         select 8 union all
         select 9
      ) n1,
      (
         select 0 as num union all
         select 1 union all
         select 2 union all
         select 3 union all
         select 4 union all
         select 5 union all
         select 6 union all
         select 7 union all
         select 8 union all
         select 9
      ) n2,
      (
         select 0 as num union all
         select 1 union all
         select 2 union all
         select 3
      ) n3
   ) gencalendar
   order by 1   
   
   -- 4 --
   -- if it was a leap year
   -- add 31/12
   if (@p_check_day = 29)  
      insert into @tbl values(convert(smalldatetime, 
                                      convert(varchar(4), 
                                      @p_year)
                                      + '1231'))   
   return
end

:)

Related Posts:

Saturday, April 10, 2004 4:14 AM

Comments

# re: Generate Calendar Dates.Final Version
/*
########################################################
#
# F_TBL_CALENDAR
#
# version #2
#
# use: select * from dbo.udf_year_dates_table(2004)
#
########################################################
*/

create function udf_year_dates_table (@year int)
returns @year_dates table(date1 datetime) as
begin
declare @cnt int, @start_date datetime
select @start_date = convert(datetime,'01/01/'+convert(varchar(4),@year),120)
select @cnt = datediff(dd,@start_date ,dateadd(yy,1,@start_date))
insert into @year_dates
select
@start_date +
n3.num * 100 +
n2.num * 10 +
n1.num as date1
from
(
select 0 as num union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9
) n1,
(
select 0 as num union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9
) n2,
(
select 0 as num union all
select 1 union all
select 2 union all
select 3
) n3
where
n3.num * 100 +
n2.num * 10 +
n1.num < @cnt
order by
@start_date +
n3.num * 100 +
n2.num * 10 +
n1.num

RETURN
end

4/12/2004 5:23 PM by Enigma

# re: Generate Calendar Dates. Version #2.
This function returns the dates for only the particular year you ask for ... no need to do a top 365 or top 366

4/12/2004 5:24 PM by Enigma

# re: Generate Calendar Dates. Version #2.
You don't need to do a top 365/366 using my function in version #2 too.

4/12/2004 11:25 PM by Anatoly Lubarsky

# re: Generate Calendar Dates. Version #2.
thanks for that:
select @cnt = datediff(dd,@start_date ,dateadd(yy,1,@start_date))

4/12/2004 11:35 PM by Anatoly Lubarsky

Login

Subscribe via RSS

Article Categories

.Net Framework
ASP.NET Tips
C# Win32 API
HTML, CSS, Web
Javascript Tips
MSSQL Tips
System
System.Net
WebServices

Archives

(02) January 2018
(01) June 2013
(03) March 2013
(02) February 2013
(01) July 2012
(01) April 2012
(01) September 2011
(01) August 2011
(03) May 2011
(01) December 2010
(01) November 2010
(01) October 2010
(01) June 2010
(01) May 2010
(02) March 2010
(01) January 2010
(02) December 2009
(03) September 2009
(03) August 2009
(09) July 2009
(04) June 2009
(03) May 2009
(02) April 2009
(03) March 2009
(02) February 2009
(02) January 2009
(04) December 2008
(04) November 2008
(05) October 2008
(04) September 2008
(05) August 2008
(04) July 2008
(05) June 2008
(07) May 2008
(04) April 2008
(03) March 2008
(02) February 2008
(03) January 2008
(03) December 2007
(05) November 2007
(04) October 2007
(05) September 2007
(12) August 2007
(11) July 2007
(14) June 2007
(13) May 2007
(13) April 2007
(10) March 2007
(11) February 2007
(14) January 2007
(14) December 2006
(12) November 2006
(08) October 2006
(09) September 2006
(06) August 2006
(08) July 2006
(10) June 2006
(09) May 2006
(22) April 2006
(25) March 2006
(12) February 2006
(14) January 2006
(19) December 2005
(17) November 2005
(16) October 2005
(16) September 2005
(12) August 2005
(14) July 2005
(09) June 2005
(12) May 2005
(12) April 2005
(20) March 2005
(11) February 2005
(12) January 2005
(18) December 2004
(13) November 2004
(12) October 2004
(14) September 2004
(09) August 2004
(23) July 2004
(19) June 2004
(29) May 2004
(19) April 2004
(16) March 2004
(09) February 2004
(06) January 2004
(02) December 2003
(01) November 2003

Post Categories

.Net and C#
Android
Antispam
App. Development
Architecture
ASP.NET
Blogging
Deprecated Projects
Facebook Platform
Fun
Google
iOS
Javascript
Misc.
MSSQL
Music
My Games
Performance
Roller
Social Networks
Tools
Visual Studio
Web 2.0
WebServices

About Me

linkedin Profile
Recs
Who am I

My Sites

Billy Beet
x2line blogs