dbTalk Databases Forums  

Way to create table of months?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Way to create table of months? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
manning_news@hotmail.com
 
Posts: n/a

Default Way to create table of months? - 08-23-2005 , 11:09 AM






Is there an way to create a table of months using SQL functions? If
"July" is entered for the beginning month, and "December" is entered
for the ending month, then I'd like to create a table with 6 rows, one
for July, August,....December.

Thanks.


Reply With Quote
  #2  
Old   
Ross Presser
 
Posts: n/a

Default Re: Way to create table of months? - 08-23-2005 , 01:21 PM






On 23 Aug 2005 09:09:51 -0700, manning_news (AT) hotmail (DOT) com wrote:

Quote:
Is there an way to create a table of months using SQL functions? If
"July" is entered for the beginning month, and "December" is entered
for the ending month, then I'd like to create a table with 6 rows, one
for July, August,....December.

Thanks.
create function dbo.MonthList
( @begMonth varchar(15),
@endMonth varchar(15)
) RETURNS @MonthList TABLE
( Num int, MonthName varchar(15) )
AS
BEGIN

declare @date1 datetime
declare @date2 datetime
set @date1 = convert(datetime,@begmonth + ' 1,2000')
set @date2 = convert(datetime,@endMonth + ' 1,2000')
IF datediff(month,@date1,@date2) < 0
set @date2 = dateadd(year,1,@date2)

INSERT @MonthList (Num, MonthName)
SELECT num, datename(month,dateadd(month,num,@date1)) AS MonthName
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 union all
select 10 union all select 11
) AS nums
WHERE dateadd(month,num,@date1) <= @date2
return
END

GO

SELECT * from dbo.MonthList('July','December')
SELECT * from dbo.MonthList('December','July')


Reply With Quote
  #3  
Old   
Ross Presser
 
Posts: n/a

Default Re: Way to create table of months? - 08-23-2005 , 01:22 PM



On 23 Aug 2005 09:09:51 -0700, manning_news (AT) hotmail (DOT) com wrote:

Quote:
Is there an way to create a table of months using SQL functions? If
"July" is entered for the beginning month, and "December" is entered
for the ending month, then I'd like to create a table with 6 rows, one
for July, August,....December.

Thanks.
Whups, forgot to post the output from those two selects at the bottom.

select * from dbo.MonthList('July','December')
Select * from dbo.MonthList('December)','July'

Num MonthName
----------- ---------------
0 July
1 August
2 September
3 October
4 November
5 December

(6 row(s) affected)

Num MonthName
----------- ---------------
0 December
1 January
2 February
3 March
4 April
5 May
6 June
7 July

(8 row(s) affected)


Reply With Quote
  #4  
Old   
Simon Hayes
 
Posts: n/a

Default Re: Way to create table of months? - 08-24-2005 , 02:35 AM



To add to Ross's comments, if you have to work with dates then you
might consider a complete calendar table:

http://www.aspfaq.com/show.asp?id=2519

Simon


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.