I'm trying to convert a date (date type) into int. This int should be something like the number of days since the 1 January 1900. How to get this in postgresql? In excel I'm getting this automatically when i concatenate a date with a string. Example : 2011/11/01 convert into int as 36831
1 Answer
Simply subtract the two dates:
select date '2011-11-01' - date '1900-01-01'
the result will be the number of days.
More details in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html
8 Comments
Mike Sherrill 'Cat Recall'
One hundred years is roughly 36,500 days, right? So the OP's arithmetic works out to about 101 years between 1900 and 2011. I don't think I trust that answer. I'd much rather trust PostgreSQL's subtraction operator.
Mike Sherrill 'Cat Recall'
From OP: "Example : 2011/11/01 convert into int as 36831"
nidazakiy
Thank you for the answer... But it's so confuse, in the excel, i can use this function: =datevalue("2011/11/01") for getting the serial number as 36831. But, in postgre... I tried the subtract as your answer, the result was not 36831 When i try this : (select date '2011-11-01' - date '1900-01-01') +2 in postgre, the result was same as 36831. Why it must add 2...i' m not sure with my query :-(
|