简体   繁体   English

如何在查询 to_timestamp Postgresql 中删除时区

[英]How to remove TimeZone in query to_timestamp Postgresql

I'm finding in Postgresql and I want to remove +9 UTC value in my query.我在 Postgresql 中找到,我想在我的查询中删除+9 UTC 值。

For example: In to_timestamp column, I want to remove +09 , and only keep 2016-02-26 00:23:44 value.例如:在 to_timestamp 列中,我想删除+09 ,只保留2016-02-26 00:23:44值。

This is my query:这是我的查询:

select t,to_timestamp(v) from c1.hour where f = 22 and date_trunc('day',t - '1 hour'::INTERVAL) = '2016-02-26 00:00:00' order by t ;

Here my result:这是我的结果: 在此处输入图片说明

And this is my result when I didn't use to_timestamp:这是我没有使用 to_timestamp 时的结果: 在此处输入图片说明

Can I get some help on this please?我可以得到一些帮助吗?

Use to_char() function to format timestamp to any desired string representation.使用to_char()函数将timestamp格式化为任何所需的字符串表示形式。 Example:例子:

SELECT to_char(now(), 'YYYY-MM-DD HH24:MI:SS')

will return 2016-02-26 09:37:49 (without a timezone)将返回2016-02-26 09:37:49 (没有时区)

Your query have to be this:您的查询必须是这样的:

SELECT t, to_char(to_timestamp(v), 'YYYY-MM-DD HH24:MI:SS') 
FROM c1.hour 
WHERE 
   f = 22 
   AND date_trunc('day',t - '1 hour'::INTERVAL) = '2016-02-26 00:00:00' 
ORDER BY t ;

Specify UTC:指定UTC:

select to_timestamp(0)::timestamptz at time zone 'UTC'

I am in timezone +08 and get the following result:我在时区 +08 并得到以下结果:

1970-01-01 00:00:00 1970-01-01 00:00:00

With the time zone specification, I get:使用时区规范,我得到:

1970-01-01 08:00:00+08 1970-01-01 08:00:00+08

An advantage is you don't have to specify the date-time format, which can cause unexpected results for users.一个优点是您不必指定日期时间格式,这可能会给用户带来意想不到的结果。 The system specified formatting is preserved.系统指定的格式被保留。

你可以做这个演员表(“你的专栏”作为时间戳)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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