I'm looking to create a date table in a postgres database. The sample data is expected to look like this:
date key = 00001
calendar_date= 1/1/2015
week_num= 1
month_num= 1
month_name= Jan
quarter_num= 1
calendar_year= 2015
iso_dayofweek= 4
dayofweek_name= Thursday
Is there a function or SQL that I can get help with to create a date gregorian ISO-8601 table?
I am looking to auto generate this if possible. Any help in this direction would be appreciated.
see the below example
Result:
You can use
generate_series()
to get all date in a year for ex:2015
this will produce date from
1/1/2015 - 31/12/2015
, and use this select instead ofSELECT now()::DATE
in the given exampleIf you want to create table for year
2015
then you can use the following queryand the table will look like
select * from mycal_2015
POSTGRESQL: EXTRACT FUNCTION
The PostgreSQL extract function extracts parts from a date
Syntax :
extract( unit from date )
It can be one of the following:
Note: Extract function is applies to PostgreSQL version 8.4 and above
Date/Time Functions and Operators
generate_series()