时区转换连接到数据表sql

时间:2014-02-21 19:23:01

标签: sql oracle join timezone subquery

我编写了一些允许我从UTC时间转换为本地时间的代码,但是我传入的时间是一个虚拟值,时区也是如此。我希望能够使用实际数据,但我不确定如何结合使用。可能是子查询?

以下是我对时区转换的查询:

Select From_Tz(Cast(To_Timestamp('17-FEB-14 04.00.00.000000000 PM',
'DD-MON-YY HH.MI.SS.FF9 AM') As Timestamp), 'UTC')
 At Time Zone 'America/New_York' As "Local Time"
 FROM DUAL;

代替'17 -FEB-14 04.00.00.000000000 PM'和'America / New_York',我想传递以下查询返回的值:

Select s.Max(Date), time.Local_Time_Zone from Sales s
join on s.customer_ID = time.customer_ID
where s.customer_ID = 122;

1 个答案:

答案 0 :(得分:0)

如果您的表中只有一条记录,这似乎很可能,那么您不需要max()或子查询,您可以直接从该表中选择,如:

select from_tz(cast(date_field as timestamp), 'UTC')
  at time zone customer_time_zone as "Local Time"
from customer_table;

我还假设您的“日期”字段实际上是DATE,而不是TIMEZONE,但这并不完全清楚。

使用格式显示:

select to_char(from_tz(cast(date_field as timestamp), 'UTC')
  at time zone customer_time_zone, 'YYYY-MM-DD HH24:MI:SS') as "Local Time"
from customer_table;

LOCAL TIME
-------------------
2014-02-16 19:00:00

SQL Fiddle


根据您更新的问题,主体是相同的,您只需要加入两个表:

select to_char(from_tz(cast(max(s.date_field) as timestamp), 'UTC')
  at time zone t.local_time_zone, 'YYYY-MM-DD HH24:MI:SS') as "Local Time"
from sales s
join time t on t.customer_id = s.customer_id
group by s.customer_id, t.local_time_zone;

SQL Fiddle