Home » excel » sql – Convert Excel date format to regular date with postgres

sql – Convert Excel date format to regular date with postgres

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have csv files loaded into a DBeaver postgres database with the dates expressed as 5 digits. (41827, 35267, 39444, etc.)
I know these are formatted in Excel as the number of days since Jan 1, 1900. Is there a query I can write in my editor to convert those into a mm/dd/yyyy?

I’ve tried using a SELECT/INTERVAL/CAST type of function but cant quite figure out the syntax.

Example: Schema: profiles,
Table: account_transactions,
Column name: date_activated,
Cell value: 41827(varchar)

How to&Answers:

Excel dates represent the number of days since December 30th, 1899 (well, approximatly – early years are inaccurate).

In Postgres you could do:

date '1899-12-30' + myexceldate::int * interval '1' day

Where myexceldate is the name of the text column that holds the excel number.