Anurag Patel
Anurag Patel

Reputation: 3

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'

Upvotes: 0

Views: 605

Answers (1)

जलजनक
जलजनक

Reputation: 3071

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

Upvotes: 1

Related Questions