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

时间:2014-03-20 19:11:32

标签: sql oracle sql-insert

我正在尝试将数据插入现有表并继续收到错误。

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,任何帮助都将不胜感激!

7 个答案:

答案 0 :(得分:91)

尝试使用'1989-12-09'

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

答案 1 :(得分:3)

您用于该日期的格式与oracle的默认日期格式不匹配。 Oracle数据库的默认安装将默认DATE格式设置为DD-MON-YYYY。你可以使用函数to_date或只使用DD-MON-YYYY日期格式模型。

答案 2 :(得分:3)

您还可以更改会话的日期格式。例如,在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

答案 3 :(得分:0)

尝试像这样保存日期 yyyy-mm-dd hh:mm:ss.ms 例如: 1992-07-01 00:00:00.0 这对我有用

答案 4 :(得分:0)

ORA-01861: literal does not match format string

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

您可以通过执行以下更改来克服此问题。

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

通常,如果您使用的是TO_DATE函数,则TO_TIMESTAMP   函数,TO_CHAR函数和类似函数,请确保   您提供的文字与您所拥有的格式字符串匹配   指定

答案 5 :(得分:-1)

请尝试使用dd-mon-yyyy格式,例如02-08-2016的格式应为' 08-feb-2016'。

答案 6 :(得分:-1)

如果您提供正确的日期格式,它应该有效,如果您在插入值中给出了正确的日期格式,请重新检查一次