Home > OS >  Cast string 'May 11 2022 9:16AM' to a timestamp
Cast string 'May 11 2022 9:16AM' to a timestamp

Time:06-14

I have this query:

select to_timestamp('May 11 2022  9:16AM', 'Month DD YYYY HH:MI')

Returns: 2022-05-11 09:16:00.000 -0300

The expression 'Month DD YYYY HH:MI' is the result of some trial and error.

I can't tell if what I have is 'right'? Expected something more like: 2022-05-11 09:16:00

What is ...00.000 -0300 this part?

How can I get a timestamp from May 11 2022 9:16AM?

CodePudding user response:

Per the docs Data Type Formatting Functions to_timestamp:

to_timestamp ( text, text ) → timestamp with time zone

Converts string to time stamp according to the given format. (See also to_timestamp(double precision) in Table 9.32.)

to_timestamp('05 Dec 2000', 'DD Mon YYYY') → 2000-12-05 00:00:00-05

So what you are seeing is a timestamptz value, e.g a timestamp with time zone information. If you want to eliminate the time zone then:

select to_timestamp('May 11 2022  9:16AM', 'Month DD YYYY HH:MI')::timestamp;
    to_timestamp     
---------------------
 2022-05-11 09:16:00


  • Related