简体   繁体   English

ORA-01861:尽管使用了to_date函数,但文字不匹配格式字符串

[英]ORA-01861: literal does not match format string despite of using to_date function

Facing this error on EXCEL VBA Module. 在EXCEL VBA模块上遇到此错误。 On Oracle 11g interface it runs well. 在Oracle 11g界面上,它运行良好。

select 
        "schema_name", "table_name", "bundle_name", "text_file", "sequence_no", 
                    lastupdateapplied, "next_sat_date","prev_sat_date", TO_DATE(SYSDATE,'DD-MM-YYYY'),
        CASE 
                WHEN ("schema_name"='EDM_V1' OR "schema_name"='FF_V3' OR "schema_name"='SYM_V1') and 
                            ("next_sat_date"-lastupdateapplied<=0 OR 
                            lastupdateapplied-"prev_sat_date">=0) 
                then 'EVERYTHING LOOKS OK HERE.'
                WHEN  ("schema_name"='FP_V2' OR "schema_name"='FE_V4' OR "schema_name"='REF_V2') and 
                            (lastupdateapplied - TO_DATE(SYSDATE,'DD-MM-YYYY') =0)
                then 'EVERYTHING LOOKS OK HERE.'
                Else 'PLEASE CHECK THE SCHEDULER FOR THIS FEED.' END as    "VERIFICATION_NOTE"
from
        ( 
            select 
                    UPPER(ds."schema_name") as "schema_name", UPPER(ds."table_name") as "table_name", ds."bundle" as "bundle_name", ds."text_file" as "text_file", 
                    ds."sequence" as "sequence_no", TO_DATE(s2.end_time,'DD-MM-YYYY') as lastupdateapplied, TO_DATE(next_day(SYSDATE,'SATURDAY'),'DD-MM-YYYY') as "next_sat_date",  
                    TO_DATE(next_day(SYSDATE,'SATURDAY')-(INTERVAL '7' DAY + INTERVAL '1' SECOND),'DD-MM-YYYY') AS "prev_sat_date"
            from FDS_FDS_DATA_SEQUENCES ds 
            Join 
            (   select "table_name", "bundle", "text_file", max("end_time") as end_time 
                from FDS_FDS_FILE_HISTORY 
                where "file_type" = 'full' 
                group by "table_name", "bundle", "text_file") s 
             on s."bundle" = ds."bundle" and s."table_name" = ds."table_name" and s."text_file" = ds."text_file"
            Join 
            (   select "table_name", "bundle", "text_file", max("end_time") as end_time 
                from FDS_FDS_FILE_HISTORY 
                where "file_type" = 'update' 
                group by "table_name", "bundle", "text_file") s2 
            on s2."bundle" = ds."bundle" and s2."table_name" =  ds."table_name" and s2."text_file" = ds."text_file" 
Order by 
            ds."schema_name" asc, ds."bundle" asc, s2.end_time desc )

Expected result is i should not get the error and actual result is ===>when i run the same query in ORACLE SQL Developer it runs perfectly but does not run in excel VBA module. 预期结果是我不应该收到错误,而实际结果是===>当我在ORACLE SQL Developer中运行相同的查询时,它可以完美运行,但不能在excel VBA模块中运行。

despite of using to_date function 尽管使用了to_date函数

Actually it's because of using the to_date() function. 实际上是因为使用了to_date()函数。 You have things like this: 你有这样的事情:

TO_DATE(SYSDATE,'DD-MM-YYYY')
TO_DATE(s2.end_time,'DD-MM-YYYY')
TO_DATE(next_day(SYSDATE,'SATURDAY'),'DD-MM-YYYY')
TO_DATE(next_day(SYSDATE,'SATURDAY')-(INTERVAL '7' DAY + INTERVAL '1' SECOND),'DD-MM-YYYY')

The second of those might be OK, but only if sd.end_time is stored as a string - which it almost certainly shouldn't be. 这些中的第二个可能是好的,但是仅当sd.end_time存储为字符串时-几乎肯定不应该这样做。

For the other three, at least, you're passing something that is already a date into a function that is there to convert from a string to a date. 至少对于其他三个,您正在将已经是日期的内容传递给可以从字符串转换为日期的函数。 So you're really doing, for example: 因此,您实际上在做,例如:

TO_DATE(TO_CHAR(SYSDATE),'DD-MM-YYYY')

and as that implicit TO_CHAR() doesn't have a format model it will use your session's NLS settings, specifically NLS_DATE_FORMAT . 并且由于隐式TO_CHAR()没有格式模型,因此它将使用会话的NLS设置,尤其是NLS_DATE_FORMAT You're seeing it work in one environment because the settings there effectively mean it's doing: 您会看到它在一种环境中工作,因为那里的设置有效地表明它在做:

TO_DATE(TO_CHAR(SYSDATE, 'DD-MM-YYYY'),'DD-MM-YYYY')

whereas in another session it might effectively be doing: 而在另一个会话中,它可能实际上是在做:

TO_DATE(TO_CHAR(SYSDATE, 'DD-MON-RR'),'DD-MM-YYYY')

... which would lose the century, leaving a date value in year 0019 instead of 2019; ...这将失去世纪,将日期值保留在0019年而不是2019年; or 要么

TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM-DD'),'DD-MM-YYYY')

which throws "ORA-01861: literal does not match format string" as you are seeing. 如您所见,它会抛出“ ORA-01861:文字与格式字符串不匹配”。

While you could try to force the NLS settings on every client and application to match what you're doing it'll go wrong eventually, and you should be doing that conversion at all. 尽管您可以尝试强制每个客户端和应用程序上的NLS设置与您的操作相匹配,但最终还是会出错,因此您应该进行所有转换。 Just use SYSDATE and other calculated directly instead; 只需使用SYSDATE和其他直接计算出的值即可; or if you're trying to get the dates with the times set to midnight truncate them: 或者,如果您想获取时间设置为午夜的日期,请截断它们:

trunc(SYSDATE)
trunc(s2.end_time)
trunc(next_day(SYSDATE,'SATURDAY'))
trunc(next_day(SYSDATE,'SATURDAY')-(INTERVAL '7' DAY + INTERVAL '1' SECOND))

Also note that the second argument to next_day() is the day name or abbreviation in the current session date language, so someone running this from a session in a different language will also see errors (like "ORA-01846: not a valid day of the week"). 另请注意, next_day()的第二个参数是当前会话日期语言中的日期名称或缩写,因此从会话中以另一种语言运行该日期的人也会看到错误(例如“ ORA-01846:不是有效的日期星期”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM