Comprehensive Analysis of ORA-01861 Error: Date Format Mismatch and Solutions

Nov 03, 2025 · Programming · 17 views · 7.8

Keywords: ORA-01861 | Date_Format | TO_DATE_Function | Oracle_Error | SQL_Insert

Abstract: This article provides an in-depth analysis of the common ORA-01861 error in Oracle databases, typically caused by mismatches between literal values and format strings. Through practical case studies, it demonstrates the root causes of the error and presents solutions using the TO_DATE function for format conversion. The paper further explores the handling of different data type literals in Oracle, including character, numeric, and datetime literals, helping readers fundamentally understand and prevent such errors.

Problem Overview

In Oracle database operations, the ORA-01861 error is a common but easily resolvable issue. The fundamental cause of this error lies in the mismatch between input literals and expected format strings, particularly when handling datetime data.

Error Case Analysis

Consider the following insert statement example:

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
);

This statement throws an ORA-01861 error during execution, primarily due to the handling of the DOB date field. Although '1989-12-09' appears to be a standard date format, Oracle database requires explicit date format specification for proper parsing.

Oracle Literal Types Analysis

In Oracle databases, literals are primarily categorized into three types:

Character Literals: Fixed strings enclosed in single quotes, such as 'BRAD', 'CHERIE', etc. When processing character data, correct quotation format must be ensured.

Numeric Literals: Data directly representing numerical values, such as integers and decimals. In the example, telephone number fields 02086950291 and 07498635200 are treated as numeric literals, but note that leading zeros may be ignored.

Datetime Literals: This is the main source of ORA-01861 errors. Oracle supports multiple datetime formats:

Solution Implementation

For date format mismatch issues, the most effective solution is to use the TO_DATE function for explicit format conversion:

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',
  TO_DATE('1989-12-09', 'YYYY-MM-DD'),
  'M',
  02086950291,
  07498635200
);

The TO_DATE function accepts two parameters: the date string and the format model. By explicitly specifying the 'YYYY-MM-DD' format, Oracle can correctly parse the date literal, avoiding format mismatch errors.

Deep Understanding of Format Matching

Oracle has strict requirements for format matching. When using the FX modifier, literals must exactly match the format string, including spaces and delimiters. For example:

-- Error example
SELECT TO_DATE('20140722', 'yyyy-mm-dd') FROM dual;
-- Correct example
SELECT TO_DATE('2014-07-22', 'yyyy-mm-dd') FROM dual;

In the first error example, the date literal lacks hyphen separators, causing format mismatch. The second example resolves the issue by adding correct separators.

Best Practice Recommendations

To avoid ORA-01861 errors, it is recommended to follow these best practices:

Always use the TO_DATE function for date input processing, especially when uncertain about the database default date format. Explicitly specify date format models to avoid relying on implicit conversions. During development, unify date format standards within the team to reduce errors caused by format inconsistencies. For international applications, consider using timezone-related datetime types.

By understanding Oracle's literal and format string matching mechanisms, developers can effectively prevent and resolve ORA-01861 errors, improving the stability and reliability of database operations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.