Simple Oracle query: literal does not match format string

落爺英雄遲暮 提交于 2019-11-28 10:37:35

问题


I want to execute a simple function in Oracle. The signature is defined as follows:

CREATE OR REPLACE FUNCTION NewCaseListForValidation
(
                             p_fromDate in DATE,
                             p_toDate in DATE,
                             p_rowCount in INT
)
RETURN
                             SYS_REFCURSOR
IS
                             return_value SYS_REFCURSOR;
...

I should be able to execute it with:

var rc refcursor
exec :rc := newcaselistforvalidation('2010-01-01','2011-01-01',100);
print :rc

But when typing "newcaselistforvalidation('2010-01-01','2011-01-01',100)", I get:

ERROR at line 1:
ORA-01861: literal does not match format string
ORA-06512: at line 1

I googled a bit and it seems I can't figure out to type the date in a correct format. Can anyone help me?


回答1:


Query NLS_PARAMETERS in Oracle- you will then be able to see what format your DB is accepting dates in.

Typically however i use the to_date() function:

to_date('01-01-2011','DD-MM-YYYY');

In the UK to input my dates.




回答2:


An alternative to the to_date() function is to use the ANSI standard format for DATE or TIMESTAMP literals:

DATE '2010-01-31'
TIMESTAMP '2010-01-31 21:22:23'

Date and time is always specified using ISO rules (YYYY-MM-DD and 24hour format for time)

This also works on a lot of other (standard compliant) DBMS.




回答3:


It's best not to rely on particular value in NLS_PARAMETERS settings, cause this will make your function break in the env with another NLS_DATE_FORMAT.

I'd explicitly specify date formatting in your function as suggested in the answer above

exec :rc := newcaselistforvalidation(to_date('2010-01-01','YYYY-MM-DD'),to_date('2011-01-01','YYYY-MM-DD'),100);



回答4:


INSERT INTO tblDate (dateStart) Values ('20-JUN-2013'); If you change month integer into a string 'DD-MON-YYYY' works as a valid data string without having to preface it with the DATE identifier.



来源:https://stackoverflow.com/questions/4884071/simple-oracle-query-literal-does-not-match-format-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!