Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 make 2006 calendar table

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-02-09 : 11:44:19
I need a table in which store entire 2006 day and day of week. (365 rows) Is it possible to code to make?

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2006-02-09 : 11:56:54
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.TableOfNumbers
GO

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 #num
GO
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 DATETIME
SET @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 ton
WHERE
ton.DummyNumber BETWEEN 0 AND 364


Mark
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-02-09 : 12:02:59
Thank you!!! I'll try it.
Go to Top of Page

dlit
Starting Member

2 Posts

Posted - 2006-02-09 : 14:36:03
This will do it - use @StartDate and @Days to set the range of dates that will be placed in the table. I added some calculated columns to the table to show how you could have the table include whatever values you want from each date too.

create table #2006Dates
([Date] smalldatetime,
[Day] AS day([Date]),
[DayOfWeek] AS datepart(dw, [Date]),
[Quarter] AS datepart(q, [Date]))
declare @StartDate smalldatetime
declare @Days int
declare @CurrentDay int

set @StartDate = '1/1/2006'
set @Days = 365
set @CurrentDay = 0

while @CurrentDay < @Days
begin
insert #2006Dates ([Date]) values (dateadd(dd, @CurrentDay, @StartDate))
set @CurrentDay = @CurrentDay + 1
end

select * from #2006Dates
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-09 : 20:24:44
quote:
Originally posted by Sun Foster

I need a table in which store entire 2006 day and day of week. (365 rows) Is it possible to code to make?


You don't need to store the day of week as it can be calculate easily using the datepart(weekday, yourdate) function.
declare
@start_date datetime

select @start_date = '2006-01-01'

select dateadd(day, NUMBER, @start_date) as dte,
datepart(weekday, dateadd(day, NUMBER, @start_date)) as [day of week]
from dbo.F_TABLE_NUMBER_RANGE(0,365)
where dateadd(day, NUMBER, @start_date) < dateadd(year, 1, @start_date)

Refer to here for MVJ's F_TABLE_NUMBER_RANGE number table function.

Actually you might not need the table at all as the data you want can be generated easily as shown above.

----------------------------------
'KH'

everything that has a beginning has an end
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-09 : 21:54:02
Also see here. Newly posted by MVJ the scriptor. Date Table Function F_TABLE_DATE

----------------------------------
'KH'

everything that has a beginning has an end
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-02-09 : 21:55:22
If you find you need a more full featured calendar table, check out this table function, F_TABLE_DATE:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519







CODO ERGO SUM
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-02-09 : 22:03:43
Why? for what purpose. The idea of having to pre-populate a table is a bad idea. What do you need to do this for?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page
   

- Advertisement -