Displays range of dates – Calendar

>>Script Language and Platform: SQL Server 2000
This script displays a range of dates.

select count(*) from dbo.calendar (‘1974-09-09′,’2004-01-10’)
–results
10716

select * from dbo.calendar (‘2001-01-01′,’2001-01-05’)

–results
id,date,month,day
id,date,month,day
1,2001-01-01 00:00:00.000,January,Monday
2,2001-01-02 00:00:00.000,January,Tuesday
3,2001-01-03 00:00:00.000,January,Wednesday
4,2001-01-04 00:00:00.000,January,Thursday
5,2001-01-05 00:00:00.000,January,Friday

select * from dbo.calendar (‘1974-09-07′,’1974-09-10’)
–results
id,date,month,day
1,1974-09-07 00:00:00.000,September,Saturday
2,1974-09-08 00:00:00.000,September,Sunday
3,1974-09-09 00:00:00.000,September,Monday
4,1974-09-10 00:00:00.000,September,Tuesday

Author: MAK


create function dbo.calendar (@startdate datetime, @enddate datetime)
returns @calendartable TABLE (id int identity(1,1),
date datetime,
month varchar(12),
day varchar(10))
as
BEGIN
–Author: MAK
–Objective: To display the calendar
–Date: April 1, 2002
declare @startdate1 datetime
set @startdate1 = @startdate
while @enddate >=@startdate1
begin
insert into @calendartable (date,month,day)
values (@startdate1,datename(month,@startdate1),datename(dw,@startdate1))
set @startdate1 =@startdate1+1
end
return
END



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles