Categories

Follow our news:

Follow canonburysvcs on Twitter Follow Canonbury Services on Facebook Follow Canonbury Services' news by RSS Follow Canonbury Services' news by Atom Follow Canonbury Services' news by email

NEWS & TECH BLOG

Creating a date range table

01/03/2011 – in SQL queries

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

create table [dbo].[daterange]
([date] [datetime] null)

DECLARE
@dDate DateTime,
@dEndDate DateTime

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

Share