简体   繁体   中英

Convert Timestamp without timezone to integer Postgres

I've inherited a database with a quirky schema. Without boring of the details I have an odd use case.

The table contains a filed completed_at that is a timestamp without timezone. What I'm trying to do is order the results by the timestamp where the timestamp is > X

The kicker is X is a numeric (or decimal) value. Trying to convert the timestamp to int is failing or even to string.

If you simply try completed_at > 0 you get

The only answer I found was in this message: https://www.postgresql.org/message-id/3EE8B4ED.1060200%40pgsqldb.com

Basically, I guess, the answer is: select cast(extract(epoch from current_timestamp) as integer) ;

我正在使用以下代码将当前时间转换为 Bigint。

SELECT cast(to_char((current_timestamp)::TIMESTAMP,'yyyymmddhhmiss') as BigInt)

我有一个类似的问题:我想将年份提取为整数,因此使用上面的内容我将CAST(EXTRACT(YEAR FROM CURRENT_TIMESTAMP) AS INTEGER)写入了我的 PostgreSQL 数据库,然后检查了 PGADMIN 4 中的约束,结果得到了以下替代方案,我觉得很有趣:

date_part('year'::text, CURRENT_TIMESTAMP)::integer

I would use the following code if it is SQL. It works for me. Change the ASC to DESC if it is the wrong way. You should not need to convert anything.

ORDER BY `timestamp_column` ASC

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