ORA-01861: Literal Does Not Match Format String

Introduction

The ORA-01861: Literal Does Not Match Format String error in Oracle, also known as "literal does not match format string," can be quite perplexing. This error occurs when there's a mismatch between the format specified in your SQL query and the actual format of the literal value provided. Understanding this error is crucial for efficient database management and avoiding common pitfalls in date and number formatting.

Causes of ORA-01861

The ORA-01861 error typically arises due to the following issues:

  • Mismatched Date Formats: The error often occurs when you use a date literal that does not conform to the date format specified in the query. For example, if you specify a format of 'MM/DD/YYYY' but provide a date in 'DD-MM-YYYY', Oracle will throw this error.
  • Incorrect Numeric Formats: Similar issues can arise with numeric values. For instance, if your format expects a decimal point but receives a comma, or if the number of digits does not match the format.
  • Improperly Formatted Strings: When a string literal contains characters that don't fit the expected format, it triggers this error. This is common when dealing with dates or numbers formatted in specific ways.

How to Fix ORA-01861

Resolving the ORA-01861 error involves aligning your literal values with the specified format strings. Here are some practical steps to fix this error:

1. Verify and Correct Date Formats

Ensure that the date literals in your SQL queries match the expected date format. For example:


        -- Incorrect Format
        SELECT TO_DATE('2024/31/12', 'YYYY-MM-DD') FROM dual;
        -- Correct Format
        SELECT TO_DATE('2024-12-31', 'YYYY-MM-DD') FROM dual;
        

In the above example, the date string '2024/31/12' does not match the format 'YYYY-MM-DD', resulting in an error. Correcting the string to '2024-12-31' aligns with the specified format.

2. Adjust Numeric Formats

When dealing with numeric values, ensure that the format specified matches the literal value:


        -- Incorrect Numeric Format
        SELECT TO_NUMBER('1,234.56', '9999.99') FROM dual;
        -- Correct Numeric Format
        SELECT TO_NUMBER('1234.56', '9999.99') FROM dual;
        

Here, '1,234.56' does not fit the format '9999.99' due to the comma. Removing the comma resolves the issue.

3. Validate String Formats

Ensure that all string literals fit the expected format. For example:


        -- Incorrect String Format
        SELECT TO_DATE('31st December 2024', 'DD-MM-YYYY') FROM dual;
        -- Correct String Format
        SELECT TO_DATE('31-12-2024', 'DD-MM-YYYY') FROM dual;
        

In this case, '31st December 2024' does not match 'DD-MM-YYYY'. Using '31-12-2024' will fix the error.

Examples

Here are some common examples illustrating how the ORA-01861 error occurs and how to resolve it:

Example 1: Date Format Mismatch


        -- Incorrect SQL Statement
        SELECT TO_DATE('2024-31-12', 'YYYY-MM-DD') FROM dual;
        -- Error: ORA-01861: literal does not match format string
        -- Correct SQL Statement
        SELECT TO_DATE('2024-12-31', 'YYYY-MM-DD') FROM dual;
        

Example 2: Numeric Format Error


        -- Incorrect SQL Statement
        SELECT TO_NUMBER('1.234,56', '9999.99') FROM dual;
        -- Error: ORA-01861: literal does not match format string
        -- Correct SQL Statement
        SELECT TO_NUMBER('1234.56', '9999.99') FROM dual;
        

Conclusion

The ORA-01861 error, "literal does not match format string," often stems from discrepancies between the specified format and the actual literal values in SQL queries. By carefully verifying date and numeric formats and ensuring consistency between literals and formats, you can effectively resolve and prevent this error. Understanding and addressing these formatting issues will lead to smoother SQL operations and fewer disruptions in your database management tasks.



Rate Your Experience

: 0 : 0


Last updated in December, 2024

Cloud Technology


Read more | Learn more

Oracle Database


Read more | Learn more

MSSQL Database


Read more | Learn more

PostGres Database


Read more | Learn more

Linux


Read more | Learn more

ASP/C#


Read more | Learn more

Online Tests


Read more | Learn more