[英]convert a string to oracle sql timestamp Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)
Greeting Community.问候社区。
I saw similar examples in Java but how do we do it it SQL in particular Oracle.我在 Java 中看到了类似的例子,但我们如何做到这一点 SQL 特别是 Oracle。 For now I just do this which it works because we are in PDT timezone.
现在我只是这样做,因为我们在 PDT 时区。
-Thx for the help! -感谢您的帮助!
with xx as (
select replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)',' GMT-0700 (PDT)','') as dt from dual
)
select to_date(dt,'DY MON DD YYYY HH24:MI:SS') from xx
The input is a timestamp with time zone.输入是带时区的时间戳。 It has redundant information about the time zone;
它有关于时区的冗余信息; some of that must be stripped away.
其中一些必须被剥离。 For example, if you choose to trust PDT and ignore the GMT offset, you could do something like this:
例如,如果您选择信任 PDT 并忽略 GMT 偏移量,您可以执行以下操作:
with xx as (
select regexp_replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)','GMT-\d{4} \(|\)')
as str_ts_with_tz
from dual
)
select to_timestamp_tz(str_ts_with_tz,'Dy Mon DD YYYY HH24:MI:SS TZD')
as oracle_ts_with_tz
from xx
;
ORACLE_TS_WITH_TZ
---------------------------------------
2019-08-19 08:21:48 America/Los_Angeles
Note that the resulting data type is timestamp with time zone .请注意,结果数据类型是带时区的时间戳。 You can further cast this as timestamp or as date (simply discarding the time zone information) if needed;
如果需要,您可以进一步将其转换为时间戳或日期(只需丢弃时区信息); this is a simple operation, internal to Oracle.
这是 Oracle 内部的一个简单操作。 But that will lose information;
但这会丢失信息; think twice before you do that.
在你这样做之前三思而后行。
Note also that the timestamp is presented in a different format in my output, compared to your input.另请注意,与您的输入相比,时间戳在我的输出中以不同的格式显示。 This is because my
NLS_TIMESTAMP_TZ_FORMAT
is 'yyyy-mm-dd hh24:mi:ss tzr'
.这是因为我的
NLS_TIMESTAMP_TZ_FORMAT
是'yyyy-mm-dd hh24:mi:ss tzr'
。
If you are wondering why you must make a choice (which, then, means "why you need to delete part of the input string first") regarding the time zone information: PDT is simply not the same as GMT-0700.如果您想知道为什么必须对时区信息做出选择(这意味着“为什么需要先删除部分输入字符串”):PDT 与 GMT-0700 完全不同。 It may be that in summer, but not in winter.
夏天可能会这样,但冬天不会。 Oracle won't accept such self-contradicting nonsense as input to its functions.
Oracle 不会接受这种自相矛盾的废话作为其功能的输入。 Either it's GMT-0700 or it's PDT, it can't be both.
要么是 GMT-0700,要么是 PDT,不能两者兼而有之。 And, you can't just use REPLACE (not easily, anyway), because what must be replaced may have variable characters in it.
而且,您不能只使用 REPLACE(无论如何都不容易),因为必须替换的内容可能包含可变字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.