SQL 错误:ORA-01861:文字与格式字符串 01861 不匹配

问题描述 投票:0回答:5

我正在尝试将数据插入现有表中,但不断收到错误。

INSERT INTO Patient  
(
  PatientNo,
  PatientFirstName,
  PatientLastName,
  PatientStreetAddress,
  PatientTown,
  PatientCounty,
  PatientPostcode,
  DOB,
  Gender,
  PatientHomeTelephoneNumber,
  PatientMobileTelephoneNumber
)
VALUES 
(
  121, 
  'Miles', 
  'Malone', 
  '64 Zoo Lane', 
  'Clapham', 
  'United Kingdom',
  'SW4 9LP',
  '1989-12-09',
  'M',
  02086950291,
  07498635200
);

错误:

Error starting at line : 1 in command -
INSERT INTO Patient (PatientNo,PatientFirstName,PatientLastName,PatientStreetAddress,PatientTown,PatientCounty,PatientPostcode,DOB,Gender,PatientHomeTelephoneNumber,PatientMobileTelephoneNumber)
VALUES (121, 'Miles', 'Malone', '64 Zoo Lane', 'Clapham', 'United Kingdom','SW4 9LP','1989-12-09','M',02086950291,07498635200)
Error report -
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.

只是不确定为什么这种情况不断发生,我现在正在学习 SQL,任何帮助将不胜感激!

sql oracle sql-insert
5个回答
223
投票

尝试将日期

'1989-12-09'
的字符串文字替换为
TO_DATE('1989-12-09','YYYY-MM-DD')


17
投票

您使用的日期格式与 Oracle 的默认日期格式不匹配。

Oracle 数据库的默认安装将默认日期格式设置为

dd-MMM-yyyy

使用函数

TO_DATE(dateStr, formatStr)
或仅使用
dd-MMM-yyyy
日期格式模型。


9
投票

您还可以更改会话的日期格式。这很有用,例如在 Perl DBI 中,其中 to_date() 函数不可用:

ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'

您也可以永久设置默认的nls_date_format:

ALTER SYSTEM SET NLS_DATE_FORMAT='YYYY-MM-DD'

在 Perl DBI 中,您可以使用 do() 方法运行这些命令:

$db->do("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD');

http://www.dba-oracle.com/t_dbi_interface1.htm https://community.oracle.com/thread/682596?start=15&tstart=0


8
投票
ORA-01861: literal does not match format string

发生这种情况是因为您尝试输入带有格式字符串的文字,但格式字符串的长度与文字的长度不同。

您可以通过进行以下更改来解决此问题。

TO_DATE('1989-12-09','YYYY-MM-DD')

作为一般规则,如果您使用 TO_DATE 函数,则 TO_TIMESTAMP 函数、TO_CHAR 函数和类似函数,请确保 您提供的文字与您的格式字符串相匹配 指定


0
投票

最好避免 TO_DATE 转换并正确使用日期文字: 日期 '1989-12-09'

© www.soinside.com 2019 - 2024. All rights reserved.