You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle插入Timestamp失败求助:ORA-01861错误修复方案

Oracle插入Timestamp类型值失败的修复方案

你遇到的ORA-01861错误是因为Oracle默认无法识别带T分隔符和Z时区标识的ISO 8601格式字符串,直接加timestamp关键字无法完成格式解析,必须用日期转换函数指定匹配的格式掩码。

方案1:针对TIMESTAMP WITH TIME ZONE类型字段

如果created_date字段是TIMESTAMP WITH TIME ZONE类型,直接用TO_TIMESTAMP_TZ函数解析带Z标识的字符串:

INSERT INTO table (name, created_date) 
VALUES ('test', TO_TIMESTAMP_TZ('2023-04-24T11:11:11.807Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"'));

格式掩码说明:

  • YYYY-MM-DD:匹配日期部分
  • "T":匹配原字符串中的固定T分隔符(双引号包裹表示固定字符)
  • HH24:MI:SS.FF3:匹配时分秒及三位毫秒
  • "Z":匹配末尾的Z标识(代表UTC时区)

方案2:针对普通TIMESTAMP类型字段

如果created_date是不带时区的TIMESTAMP类型,可以先将带Z的字符串转换为UTC时区时间戳,再转为普通TIMESTAMP:

INSERT INTO table (name, created_date) 
VALUES ('test', CAST(TO_TIMESTAMP_TZ('2023-04-24T11:11:11.807Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"') AS TIMESTAMP));

或者直接去掉Z标识,用TO_TIMESTAMP解析:

INSERT INTO table (name, created_date) 
VALUES ('test', TO_TIMESTAMP('2023-04-24T11:11:11.807', 'YYYY-MM-DD"T"HH24:MI:SS.FF3'));

为什么直接加timestamp关键字无效?

Oracle中timestamp关键字仅用于声明类型或简单日期字面量(如TIMESTAMP '2023-04-24 11:11:11.807'),但无法识别带T和Z的特殊格式,必须通过转换函数指定格式规则才能正确解析。


原报错SQL

insert into table (name, created_date) values ('test', '2023-04-24T11:11:11.807Z');

抛出异常

SQL Error: ORA-01861: literal does not match format string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace). If the
"FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal.

内容的提问来源于stack exchange,提问作者anurag1007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 01:28:26