Oracle - 将字符串转换为时间戳

标签 oracle

我有这个时间戳字符串:

'2021-09-07T18:24:25.075+02:00'

应转换为以下格式:

'dd.mm.yyyy hh.mi.ss'

到目前为止已经知道了,但是 Oracle 似乎无法识别该格式。有什么想法吗?

select to_char(to_timestamp('2021-09-07T18:24:25.075+02:00', 'yyyy-mm-ddThh:mi:ss'), 'dd.mm.yyyy hh.mi.ss') from dual

最佳答案

在格式模型内的文字字符周围使用双引号,并将 TO_TIMESTAMP_TZTZH:TZM 格式模型一起使用以匹配时间戳(并使用 FF 来匹配小数秒和 24 小时制的 HH24):

select to_char(
         to_timestamp_tz(
           '2021-09-07T18:24:25.075+02:00',
           'yyyy-mm-dd"T"hh24:mi:ss.ffTZH:TZM'
         ),
         'dd.mm.yyyy hh24.mi.ss'
       ) AS timestamp
from   dual

输出:

TIMESTAMP
07.09.2021 18.24.25

db<> fiddle here


您在评论中提问:

Gotta deal with an ORA-01830 "Oracle date format picture ends before converting entire input string" error now though. ... That is, when I switch the timestamp string with the column I try to convert

从 Oracle 12 开始,您可以使用:

select to_char(
         to_timestamp_tz(
           column_name
           DEFAULT NULL ON CONVERSION ERROR,
           'yyyy-mm-dd"T"hh24:mi:ss.ffTZH:TZM'
         ),
         'dd.mm.yyyy hh24.mi.ss'
       ) AS timestamp
from   table_name

然后,列中与您的格式模型不匹配的所有值都将返回 NULL 值。您可以使用它进行调试并查找包含无效数据的行。

select column_name
from   table_name
WHERE  to_timestamp_tz(
         column_name
         DEFAULT NULL ON CONVERSION ERROR,
         'yyyy-mm-dd"T"hh24:mi:ss.ffTZH:TZM'
       ) IS NULL;

关于Oracle - 将字符串转换为时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69673669/

相关文章:

java - 线程中的异常 "main"java.sql.SQLException : invalid name pattern: pakagename. sqlTypename

oracle - dbms_aq.dequeue_array,第一条消息返回两次

sql - Oracle PL/SQL 字符串格式化

python - cx_Oracle 游标忘记聚合时的列比例

sql - 使用预定义的系统函数将分隔字符串(或列)转换为 Oracle 中的行

oracle - 建立连接时,Oracle JDBC客户端会加密密码吗?

sql - 具有空值的多行 - 我想要一行不为空

oracle - Oracle ADF 11g 在 Java EE 框架中处于什么位置?

oracle - 在 sqlplus 中假脱机 csv 文件时的 header 格式

sql - 获取当月的当前天数(节假日除外)