473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Aug 23 '05 #1
3 8906
On 23 Aug 2005 09:09:51 -0700, ma**********@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','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)
Aug 23 '05 #2
On 23 Aug 2005 09:09:51 -0700, ma**********@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,@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')
Aug 23 '05 #3
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

Aug 24 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew | last post by:
I have a problem creating mySQL tables with PHP. I am making an app where a user can create a project. Pressing "submit" on proj_form.php goes to proj_add.php where a couple of things happen. ...
0
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
4
by: Michael Jackson | last post by:
I have a stored procedure to create a table. In my program I want the user to name the table to be created, therefore I pass a parameter to the SP for the table name. I cannot get it to work. It...
2
by: Karen Sullivan | last post by:
Hi, all. I'm fairly new to SQL, and I have been trying to create a table from a text file. I have been looking at this for days, and can't find the problem. I get a syntax error " Line 55:...
6
by: Bruce | last post by:
I want to create a new table based on an existing table, but I don't want the tables to have any enforced relationship. Is this possible without having to do a CREATE TABLE and an INSERT? ...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
1
by: Alfa & Omega | last post by:
I have app and using Microsoft Access database (mdb).. Now, after two months I figured out that I need one more Table in my database (yeah, stupid :) ). Is there any way to create a new table in...
6
by: Peter Nurse | last post by:
For reasons that are not relevant (though I explain them below *), I want, for all my users whatever privelige level, an SP which creates and inserts into a temporary table and then another SP...
3
by: SteveP26 | last post by:
Hi guys, I keep getting this error message (below) when I try to run the sql query for my database Heres the code, but the error message only applies to the LAST table (SalesCopy), I have no...
2
jbt007
by: jbt007 | last post by:
All, Access 2003 - WinXP I thought this would be a no brainer, but it seems to be a perplexing problem. I have a simple table I use for importing several text reports, use VBA to run through...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.