Displays range of dates - Calendar | Database Journal

Displays range of dates – Calendar

Apr 12, 2004
1 minute read

>>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

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.