Use a table of numbers with DATEADD. You can generate a table of numbers using the following code:-- Create Number Table IF EXISTS ( SELECT 1 FROM dbo.sysobjects WHERE id = OBJECT_ID(N'dbo.TableOfNumbers') AND OBJECTPROPERTY(id, N'IsTable') = 1 ) DROP TABLE dbo.TableOfNumbersGO CREATE TABLE dbo.TableOfNumbers ( DummyNumber INT )GO-- Create work table populated with 0-9 IF EXISTS ( SELECT 1 FROM tempdb.dbo.sysobjects WHERE name LIKE '#num%' ) DROP TABLE #numGO SELECT '0' AS num INTO #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'-- Use cross join to get 0-999 -- join in further instances of temp table-- to get more numbers if required INSERT INTO dbo.TableOfNumbers SELECT CAST(n1.num + n2.num + n3.num AS INT) FROM #num AS n1 CROSS JOIN #num AS n2 CROSS JOIN #num AS n3 ORDER BY n1.num, n2.num, n3.num
Then select from this into a new table using appropriate date functions. I.e.DECLARE @StartDate DATETIMESET @StartDate = '1/Jan/2006'SELECT DATEADD(day, ton.DummyNumber, @StartDate) AS FullDate, DATENAME(weekday,DATEADD(day, ton.DummyNumber, @StartDate)) AS DayOfWeek FROM dbo.TableOfNumbers AS tonWHERE ton.DummyNumber BETWEEN 0 AND 364
Mark