我有这个时间戳字符串:
'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_TZ
与 TZH: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/