简体   繁体   English

将带时区的 Varchar 转换为 Oracle 中的时间戳

[英]Convert Varchar with timezone to Timestamp in Oracle

I have a problem converting varchar to DateTime/timestamp.我在将 varchar 转换为 DateTime/timestamp 时遇到问题。 Here is the case这是案例

ID    EVENT_TIME(Varchar)
1    2020-04-12T09:25:53+0800
2    2020-04-12T09:25:53+0700
3    2020-04-12T09:25:53+0900

return I want, all timestamp convert to +0700返回我想要的,所有时间戳都转换为+0700

ID    EVENT_TIME(Datetime)
1    2020-04-12 10:25:53 
2    2020-04-12 09:25:53
3    2020-04-12 11:25:53

is this possible?这可能吗? and how can I do it using oracle?以及如何使用 oracle 来做到这一点?

thanks谢谢

Please use below query to convert varchar to timestamp using to_timestamp_tz and again convert it to the required time format using to_char请使用以下查询使用to_timestamp_tz将 varchar 转换为时间戳,然后使用to_char再次将其转换为所需的时间格式

select ID, to_char(to_timestamp_tz(EVENT_TIME, 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from table_name;

Example:例子:

select to_char(to_timestamp_tz('2020-04-12T09:25:53+0800', 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from dual;

在此处输入图像描述

As @Jim said, you can use to_timestamp_tz() with a character literal to convert the string to a timestamp value:正如@Jim 所说,您可以使用带有字符文字的to_timestamp_tz()将字符串转换为时间戳值:

to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM')

The to normalise to the +07:00 offset you can use at time zone :标准化为您可以at time zone使用的 +07:00 偏移量:

to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'

If you don't want to keep the time zone part you can cast to a plain timestamp:如果您不想保留时区部分,您可以转换为纯时间戳:

cast(
  to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
  as timestamp)

Or you can cast (... as date) as you don't have fractional seconds.或者您可以cast (... as date)因为您没有小数秒。

Or you can convert that back to a string for display:或者您可以将其转换回字符串以进行显示:

to_char(
  to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00',
  'SYYYY-MM-DD HH24:MI:SS')

db<>fiddle db<>小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM