简体   繁体   中英

How to convert YYYY-MM-DDTHH:mm:ss.SSSZ timestamp to YYYY-MM-DD HH:mm:ss in Postgres

Problem Summary

I would like to convert and validate the timestamp which is in YYYY-MM-DDTHH:mm:ss.SSSZ format (ex: 2022-06-15T08:27:00.599Z) in postgres.

I tried

select * from my_table WHERE my_date >= date_trunc('second', '2022-06-15T08:27:00.599Z'::timestamp)

select to_char(to_timestamp('2022-06-15T08:27:00.599Z','YYYY-MM-DD\"T\"HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')

These queries are executing fine in db_fiddle but giving an errors while running in python and postgres SQL.

Facing errors

RROR: TEIID31100 Parsing error: Encountered "'2022-06-15T08:27:00.599Z','YYYY-MM-DD"T"HH24:MI:SS), '[ ]YYYY[ ]-MM" at line 1, column 672.Was expecting: "and" | "between" | "in" | "is" | "like" | "like_regex" | "not" | "or" | "order" | "similar" ...org.teiid.jdbc.TeiidSQLException: TEIID31100 Parsing error: Encountered "'2022-06-15T08:27:00.599Z','YYYY-MM-DD"T"HH24:MI:SS), '[ ]YYYY[ ]-MM" at line 1, column 672.Was expecting: "and" | "between" | "in" | "is" | "like" | "like_regex" | "not" | "or" | "order" | "similar" ...;Error while executing the query nil

My need

Input timestamp : 2022-06-15 T 08:27:00.599 Z
Expected timestamp : 2022-06-15 08:27:00

Appreciate your support.

Forgive me if I am oversimplifying your question, but wouldn't a simple cast and date_trunc do the trick?

SELECT date_trunc('second','2022-06-15T08:27:00.599Z'::timestamp);

     date_trunc      
---------------------
 2022-06-15 08:27:00

An alternative is to round the seconds with ::timestamp(0) - see this other answer . But it would return a different result:

SELECT '2022-06-15T08:27:00.599Z'::timestamp(0);

      timestamp      
---------------------
 2022-06-15 08:27:01

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM