snippetMinor
How to store a date without the year component
Viewed 0 times
withouttheyeardatestorehowcomponent
Problem
I have to store climate (temperature) data for a few cities. This would mean that each city will have 365 values (ex: The value for Jan 1, is the average temperature for that city for all Jan 1 for 50 years (1955-2004).
Hence I am not at all concerned with year. How do I store the date in a date column? Do I store Jan 1 as Jan 1 2009 ( a non-leap year)? Or is there any better way out?
Hence I am not at all concerned with year. How do I store the date in a date column? Do I store Jan 1 as Jan 1 2009 ( a non-leap year)? Or is there any better way out?
Solution
a different approach
you can have a separate table to store all the dates, just like a Date dimension table and then in your main table have a foreign key pointing to it
Set the Dates Start and End
Generate the Date Records
View the Dates and notice that leap years have feb 29 and non leap years have only till feb 28
a non leap year
a leap year
you can use the DateID as a foreign key in your main table
This way you have day, month and year seperate and you also have the Date in dateformat for any date related functions
for reference
the script to generate a date dimension i used this site
http://www.sqlbook.com/Data-Warehousing/Date-Dimension-SQL-script-18.aspx
you can have a separate table to store all the dates, just like a Date dimension table and then in your main table have a foreign key pointing to it
CREATE TABLE YourDatesTable (
DateID int NOT NULL IDENTITY(1, 1),
[Date] datetime NOT NULL,
[Year] int NOT NULL,
[Month] int NOT NULL,
[Day] int NOT NULL
CONSTRAINT PK_Dates PRIMARY KEY CLUSTERED (DateID)
)Set the Dates Start and End
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '01/01/1955'
SET @EndDate = '12/31/2004'Generate the Date Records
DECLARE @LoopDate datetime
SET @LoopDate = @StartDate
WHILE @LoopDate <= @EndDate
BEGIN
INSERT INTO YourDatesTable VALUES (
@LoopDate,
Year(@LoopDate),
Month(@LoopDate),
Day(@LoopDate)
)
SET @LoopDate = DateAdd(d, 1, @LoopDate)
ENDView the Dates and notice that leap years have feb 29 and non leap years have only till feb 28
a non leap year
SELECT * FROM YourDatesTable
where Month = 2
and Year = 2003a leap year
SELECT * FROM YourDatesTable
where Month = 2
and Year = 2004you can use the DateID as a foreign key in your main table
This way you have day, month and year seperate and you also have the Date in dateformat for any date related functions
for reference
the script to generate a date dimension i used this site
http://www.sqlbook.com/Data-Warehousing/Date-Dimension-SQL-script-18.aspx
Code Snippets
CREATE TABLE YourDatesTable (
DateID int NOT NULL IDENTITY(1, 1),
[Date] datetime NOT NULL,
[Year] int NOT NULL,
[Month] int NOT NULL,
[Day] int NOT NULL
CONSTRAINT PK_Dates PRIMARY KEY CLUSTERED (DateID)
)DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '01/01/1955'
SET @EndDate = '12/31/2004'DECLARE @LoopDate datetime
SET @LoopDate = @StartDate
WHILE @LoopDate <= @EndDate
BEGIN
INSERT INTO YourDatesTable VALUES (
@LoopDate,
Year(@LoopDate),
Month(@LoopDate),
Day(@LoopDate)
)
SET @LoopDate = DateAdd(d, 1, @LoopDate)
ENDSELECT * FROM YourDatesTable
where Month = 2
and Year = 2003SELECT * FROM YourDatesTable
where Month = 2
and Year = 2004Context
StackExchange Database Administrators Q#18389, answer score: 3
Revisions (0)
No revisions yet.