我有 UTC 时区的日期时间,并希望将其转换为美国日期时间格式 MM/DD/YYYY 并调整为本地时区。
通过脚本在几个步骤中将其转换...
您认为可以使用 SELECT 语句来实现吗?不确定我们是否可以知道服务器上配置的时区。很高兴得到一些建议。谢谢。
答案 0 :(得分:0)
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
我们完成了