我有一个包含timestamp without time zone
列的表格(输入的数据假定位于Australia/Sydney
时区内)。
在America/New_York
时区内查询时间范围(即上午8点至下午4点)的数据。
有没有简单的方法来实现这一目标?
谢谢,p。
答案 0 :(得分:3)
想出来。
您需要先将时间转换为with time zone
版本my_ts at time zone 'Australia/Sydney'
,然后通过at time zone 'America/New_York'
select
my_ts as "Default(syd)",
my_ts at time zone 'Australia/Sydney' as "SYD",
my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York' as "NY",
date_part('hour', my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York') as "NY-hr"
from my_table
where date_part('hour', my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York')>=8
and date_part('hour', my_ts at time zone 'Australia/Sydney' at time zone 'America/New_York')<16
答案 1 :(得分:0)
您可以将所有内容转换为相同的时区,以便与之比较(如果设置了时区):
select current_time, current_time at time zone 'gmt';
timetz | timezone
-------------------+-------------------
20:50:51.07742-07 | 03:50:51.07742+00
如果未设置时区,您需要在当地时间更正:
select now()::time, now()::time + '+8:00'::interval;
now | ?column?
-----------------+-----------------
20:57:49.420742 | 04:57:49.420742
一旦你得到你想要的时间,只需提取小时,你就可以用一个简单的条件来选择合适的时间。
select *
from
(select extract(hour from now()::time + '+8:00'::interval) as hour) as t
where hour between 8 and 16;