简体   繁体   中英

Compare a character varying with a timestamp

I'm trying to compare a character varying with a timestamp in postgresSQL.

I want to get all the values that are before current time compared to the character varying timestamp.

Would this compare correctly?

WHERE to_timestamp('2018-12-25T06:00:00+01:00', 'YYYY-MM-DD HH24:MI:SS') < now()

Not exactly. There are timezone values in the literals which should be parsed. Compare:

select
    to_timestamp('2018-12-25T06:00:00+01:00', 'YYYY-MM-DD HH24:MI:SS') as "seems ok",
    to_timestamp('2018-12-25T07:00:00+02:00', 'YYYY-MM-DD HH24:MI:SS') as "but this is wrong!",
    to_timestamp('2018-12-25T06:00:00+01:00', 'YYYY-MM-DD HH24:MI:SS+TZH:TZM') as "ok",
    to_timestamp('2018-12-25T07:00:00+02:00', 'YYYY-MM-DD HH24:MI:SS+TZH:TZM') as "ok too"

        seems ok        |   but this is wrong!   |           ok           |         ok too         
------------------------+------------------------+------------------------+------------------------
 2018-12-25 06:00:00+01 | 2018-12-25 07:00:00+01 | 2018-12-25 06:00:00+01 | 2018-12-25 06:00:00+01
(1 row)

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