NEWS & TECH BLOG
Creating a date range table
01/03/2011 – in SQL queriesSometimes when writing a report in Crystal you find that you need a table that contains a sequential series of dates.
This simple query will generate one for you. Change the database name and the beginning and end dates to suit your needs.
use goldminecreate table [dbo].[daterange]
([date] [datetime] null)DECLARE
@dDate DateTime,
@dEndDate DateTimeSET @dDate = '20100101'
SET @dEndDate = '20151231'-- Populate the table with dates
WHILE @dDate < @dEndDate BEGIN INSERT INTO dbo.daterange Values(@dDate) SET @dDate = DATEADD(DAY, 1, @dDate) END