Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Way to create table of months?
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.


Report this thread to moderator Post Follow-up to this message
Old Post
manning_news@hotmail.com
08-23-05 04:24 PM


Re: Way to create table of months?
On 23 Aug 2005 09:09:51 -0700,  manning_news@hotmail
.com wrote:

> 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','De
cember')
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)

Report this thread to moderator Post Follow-up to this message
Old Post
Ross Presser
08-24-05 01:24 AM


Re: Way to create table of months?
On 23 Aug 2005 09:09:51 -0700,  manning_news@hotmail
.com wrote:

> 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,@be
gmonth + ' 1,2000')
set @date2 =  convert(datetime,@en
dMonth + ' 1,2000')
IF  datediff(month,@date
1,@date2) < 0
set @date2 =  dateadd(year,1,@date
2)

INSERT @MonthList (Num, MonthName)
SELECT num,  datename(month,datea
 dd(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,@d
ate1) <= @date2
return
END

GO

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

Report this thread to moderator Post Follow-up to this message
Old Post
Ross Presser
08-24-05 01:24 AM


Re: Way to create table of months?
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


Report this thread to moderator Post Follow-up to this message
Old Post
Simon Hayes
08-24-05 08:24 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 11:38 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006