首页 > 解决方案 > Redshift - 在 where 子句中将 UTC 时间转换为本地时间时出错

问题描述

我有一些以 UTC 记录的销售数据。我正在尝试将其转换为发生销售的当地时区。

我已经建立了一个如下查询,但收到一个错误,提示操作无效:函数 to_char(timestamp without time zone, charcter varying, unknown") 不存在。

select fs.sale_id,fs.store_type,fs.sale_time ,
case when fs.timezone = 'BST' then dateadd( h, 1, fs.sale_time ) when fs.timezone = 'EDT' then dateadd( h,- 4, fs.sale_time ) when fs.timezone = 'CEST' then dateadd( h, 2, fs.sale_time ) when fs.timezone = 'EEST' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'MSK' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'WEST' then dateadd( h, 1, fs.sale_time ) else null end, fs.timezone as new_time
from sales fs
where to_char((case when fs.timezone = 'BST' then dateadd( h, 1, fs.sale_time ) when fs.timezone = 'EDT' then dateadd( h,- 4, fs.sale_time ) when fs.timezone = 'CEST' then dateadd( h, 2, fs.sale_time ) when fs.timezone = 'EEST' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'MSK' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'WEST' then dateadd( h, 1, fs.sale_time ) else null end, fs.timezone),'yyyy-mm-dd') = '2018-09-01'

谁能建议我如何修改此查询。我正在使用红移数据库。谢谢。

标签: sqlamazon-redshifttimestamp-with-timezone

解决方案


我建议使用 'CONVERT_TIMEZONE' 函数,详细信息如下所示。

https://docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.html

例如,将销售额从 UTC 转换为 EST 的简单查询如下所示。

select listtime, convert_timezone('PST', listtime) from listing where listid = 16;

它将返回如下所示的内容。

 listtime       |   convert_timezone
 --------------------+-------------------
2008-08-24 09:36:12     2008-08-24 01:36:12 

推荐阅读