简体   繁体   English

将字符串转换为 oracle sql 时间戳 Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)

[英]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.

相关问题 SQL-将月份字符串'08 -2019'转换为时间戳数据类型-01-08-2019 - SQL - convert month string '08-2019' to timestamp datatype - 01-08-2019 SQL查询将2019/05/21 00:00:00格式转换为19-JUN-19(即DD-MON-YY) - SQL Query to Convert 2019/05/21 00:00:00 format to 21-JUN-19(i.e., DD-MON-YY) 如何在ORACLE SQL中将TIMESTAMP_WITH_TIMEZONE转换为GMT TIMESTAMP - How to convert TIMESTAMP_WITH_TIMEZONE to GMT TIMESTAMP in ORACLE SQL 如何在 oracle sql 中将 Mon Dec 21 00:00:00 EST 2020 转换为 dd/mm/yyyy - How to convert Mon Dec 21 00:00:00 EST 2020 into dd/mm/yyyy in oracle sql SQL Server日期存储为“ 2016年8月19日星期五,格林尼治标准时间-0400” - SQL Server dates stored as “Fri Aug 19 2016 01:00:00 GMT-0400” 如何将日期 2019 年 11 月 8 日转换为 11/08/19 - How to convert Date November 8 2019 to 11/08/19 如何在Oracle中将日期格式从'1965-08-01 00:00:00.0'更改为'PDT 2000年8月1日星期二00:00:00' - How to change date format from '1965-08-01 00:00:00.0' to 'Tue Aug 01 00:00:00 PDT 2000' in Oracle 将每小时时间戳从 GMT 转换为 PDT 的问题 - Issues with converting the hourly timestamp from GMT to PDT 使用sql格式化字符串日期,例如“ Mon Sep 11 2017 00:00:00 GMT + 0200(CEST)” - Format string date like this “Mon Sep 11 2017 00:00:00 GMT+0200 (CEST)” with sql 如何在Oracle SQL中将PDT转换为日期时间对象? - How to convert PDT into a date time object in Oracle sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM