[TL;DR]使用时间戳文字来避免整个问题:
INSERT INTO MY_TABLE_NAME (
UPDATED_DATE,
CREATED_DATE,
TEST_SUBJECT,
THIRD_DATE
) VALUES (
TIMESTAMP '2018-05-31 14:45:32.000',
TIMESTAMP '2018-05-31 14:45:32.000',
'test',
TIMESTAMP '2018-06-09 14:45:00.000'
);
如果你不能然后检查NLS_NUMERIC_CHARACTERS
数据库/会话参数。如果小数点不是.
,则x
格式模型将不匹配 a.
但将匹配数据库/会话正在使用的任何字符,并且字符串将不匹配。
SQL小提琴
Oracle 11g R2 模式设置:
CREATE TABLE MY_TABLE_NAME (
UPDATED_DATE TIMESTAMP,
CREATED_DATE TIMESTAMP,
TEST_SUBJECT VARCHAR2(20),
THIRD_DATE TIMESTAMP
);
查询 1:
-- Set decimal separator to "." and thousands separator to ","
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = '.,'
INSERT INTO MY_TABLE_NAME (
UPDATED_DATE,
CREATED_DATE,
TEST_SUBJECT,
THIRD_DATE
) VALUES (
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
'test',
TO_TIMESTAMP('2018-06-09 14:45:00.000', 'YYYY-MM-DD HH24:MI:SSxFF')
)
SELECT * FROM MY_TABLE_NAME
结果:
| UPDATED_DATE | CREATED_DATE | TEST_SUBJECT | THIRD_DATE |
|-----------------------|-----------------------|--------------|-----------------------|
| 2018-05-31 14:45:32.0 | 2018-05-31 14:45:32.0 | test | 2018-06-09 14:45:00.0 |
查询 2:
-- Set decimal separator to "," and thousands separator to " "
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = ', '
INSERT INTO MY_TABLE_NAME (
UPDATED_DATE,
CREATED_DATE,
TEST_SUBJECT,
THIRD_DATE
) VALUES (
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
TO_TIMESTAMP('2018-05-31 14:45:32.000', 'YYYY-MM-DD HH24:MI:SSxFF'),
'test',
TO_TIMESTAMP('2018-06-09 14:45:00.000', 'YYYY-MM-DD HH24:MI:SSxFF')
)
结果:
ORA-01830: date format picture ends before converting entire input string
另一种解决方案是'YYYY-MM-DD HH24:MI:SS.FF'
用作格式模型,而不是依赖x
格式模型在实例/会话之间始终保持一致。