Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Can';t从postgres时间戳中提取时区\小时_Sql_Postgresql - Fatal编程技术网

Sql Can';t从postgres时间戳中提取时区\小时

Sql Can';t从postgres时间戳中提取时区\小时,sql,postgresql,Sql,Postgresql,错误:不支持时间戳单位“时区” 这是所有时区字段的错误 以下是您可以运行的最小查询: select extract(timezone_hour from now()::timestamptz at time zone 'US/Eastern'); 我想要的是utc偏移量(以小时为单位)。所以这个应该返回-4 要求是在查询中使用动态时区。所以我有一个带有时区字符串的“设施”表,我需要得到每个设施的当前时间 因此,我的结束查询应该如下所示: SELECT EXTRACT(timezone_

错误:不支持时间戳单位“时区”

这是所有时区字段的错误

以下是您可以运行的最小查询:

select extract(timezone_hour from now()::timestamptz at time zone 'US/Eastern');
我想要的是utc偏移量(以小时为单位)。所以这个应该返回-4

要求是在查询中使用动态时区。所以我有一个带有时区字符串的“设施”表,我需要得到每个设施的当前时间

因此,我的结束查询应该如下所示:

SELECT
    EXTRACT(timezone_hour from now() with time zone timezone)  # timezone is the name of the field
FROM facilities;
我以为我有这一秒,但这给了我当前的偏移量,而不是我通过的tz的偏移量:

select 
    extract(timezone_hour from (select now()::timestamp at time zone 'US/Eastern'))

 date_part 
-----------
        -7
最后,我创建了两个时间戳,一个在utc,另一个在所需的时区,以实现这一点,但我将保留此选项,以防有比我当前的解决方案更好的解决方案:

select 
    extract(hour from (
                   select (
                       select now() at time zone 'US/Eastern') - (select now() at time zone 'UTC')));

 date_part 
-----------
        -4

我认为你的解决方案很好。谢谢,@LaurenzAlbe,那么我会坚持下去。看起来我应该可以在一个时间戳上使用extract,但是这并没有给聚合增加很多工作,所以我不太担心。