Skip to content
Advertisement

How to convert this string into timestamp in postgresql?

I want to convert a string into timestamp I am using the following command for it

SELECT To_timestamp ('12-31-2021-23-38-02', 'DD-MM-YYYY-HH24-MI-SS');  

However it is returning me ‘2023-07-17 23:38:02’ this instead of ‘2021-31-12 23:38:02’

Advertisement

Answer

Place of month & day in the input string doesn’t match format string.

Use:

SELECT To_timestamp ('12-31-2021-23-38-02', 'MM-DD-YYYY-HH24-MI-SS');

As pointed out in the comments, it’s apparent that you’re using Postgres prior to version 10.

Since Postgres 10, your query throws below error:

ERROR:  date/time field value out of range: "12-31-2021-23-38-02"

dbfiddle.uk – Postgres 10

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement