Bhimasen
Bhimasen

Reputation: 757

convert a date string to datetime compatible to postgresql

I have a date string in the format,

"dd/mm/yyyy"
e.g="23/2/2017"

How can I convert this to a valid format, so that I can save this value in Datetime field of postgresql.

I tried using datetime package, but not getting.

Upvotes: 6

Views: 23358

Answers (2)

ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE using to_date(<columnname>, 'DD-MM-YYYY');

Upvotes: 2

Vao Tsun
Vao Tsun

Reputation: 51456

in Postgres you can "adjust" your datestyle, eg:

t=# set datestyle TO DMY;
SET
Time: 0.215 ms
t=# select '23/2/2017'::timestamptz;
      timestamptz
------------------------
 2017-02-23 00:00:00+00
(1 row)

or just "parse" with right mask (ignoring not suitable datestyle), eg:

t=# select to_timestamp('23/2/2017','DD/MM/YYYY');
      to_timestamp
------------------------
 2017-02-23 00:00:00+00
(1 row)

Upvotes: 9

Related Questions