Oracle - 将 UTC 日期转换为美国本地日期时间格式

问题描述 投票:0回答:2

我有 UTC 时区的日期时间,希望将其转换为美国日期时间格式 MM/DD/YYYY 并调整为本地时区。

通过脚本通过几个步骤将其转换...

您认为可以使用 SELECT 语句来做到这一点吗?不确定我们是否可以知道服务器上配置的时区。很高兴得到一些建议。谢谢。

sql oracle oracle11g
2个回答
2
投票
Here is a step-by-step of the process

 I will start with a timestamp in UTC

    SQL> select timestamp '2021-07-01 09:00:00 +00:00' ts from dual;
    
    TS
    ---------------------------------------------------------------------
    01-JUL-21 09.00.00.000000000 AM +00:00

Assuming this is stored in some sort of table, the first thing we can do is convert it to the desired time zone. In my case, I live in Perth, Australia so I do:

    SQL> with my_table as (
      2  select timestamp '2021-07-01 09:00:00 +00:00' ts from dual
      3  )
      4  select ts at time zone 'Australia/Perth' as perth_time
      5  from my_table;
    
    PERTH_TIME
    ---------------------------------------------------------------
    01-JUL-21 05.00.00.000000000 PM AUSTRALIA/PERTH

9am UTC is 5pm in Perth (we're 8 hours in front).  Now I want that output in a format that I want, so I can TO_CHAR that in the normal way

SQL> with my_table as (
  2  select timestamp '2021-07-01 09:00:00 +00:00' ts from dual
  3  )
  4  select to_char(ts at time zone 'Australia/Perth','MM/DD/YYYY HH24:MI:SS') as perth_time
  5  from my_table;

PERTH_TIME
-------------------
07/01/2021 17:00:00

我们就完成了


0
投票

建议的解决方案适用于单个日期。如何使其适用于表中的列?我有一个包含 UTC 格式日期的列,我想将此列转换为时区格式

© www.soinside.com 2019 - 2024. All rights reserved.