Ошибка ora 01858

I am getting the error in the below sql:

ORA-01858: a non-numeric character was found where a numeric was expected

SELECT   c.contract_num,
         CASE
            WHEN   (  MAX (TO_CHAR (TO_DATE (c.event_dt, 'YYYY-MM-DD'), 'MMDD'))
                    - MIN (TO_CHAR (TO_DATE (c.event_dt, 'YYYY-MM-DD'), 'MMDD')))
                 / COUNT (c.event_occurrence) < 32
            THEN
              'Monthly'
            WHEN       (  MAX (
                            TO_CHAR (TO_DATE (c.event_dt, 'YYYY-MM-DD'), 'MMDD'))
                        - MIN (
                            TO_CHAR (TO_DATE (c.event_dt, 'YYYY-MM-DD'), 'MMDD')))
                     / COUNT (c.event_occurrence) >= 32
                 AND   (  MAX (
                            TO_CHAR (TO_DATE (c.event_dt, 'YYYY-MM-DD'), 'MMDD'))
                        - MIN (
                            TO_CHAR (TO_DATE (c.event_dt, 'YYYY-MM-DD'), 'MMDD')))
                     / COUNT (c.event_occurrence) < 91
            THEN
              'Quarterley'
            ELSE
              'Yearly'
         END
FROM     ps_ca_bp_events c
GROUP BY c.contract_num;

Boneist's user avatar

Boneist

22.9k1 gold badge25 silver badges40 bronze badges

asked Apr 24, 2015 at 16:08

Pradeep Choubey's user avatar

Pradeep ChoubeyPradeep Choubey

2131 gold badge2 silver badges4 bronze badges

3

The error you are getting is either because you are doing TO_DATE on a column that’s already a date, and you’re using a format mask that is different to your nls_date_format parameter[1] or because the event_occurrence column contains data that isn’t a number.

You need to a) correct your query so that it’s not using TO_DATE on the date column, and b) correct your data, if event_occurrence is supposed to be just numbers.

And fix the datatype of that column to make sure you can only store numbers.

[1] What Oracle does when you do: TO_DATE(date_column, non_default_format_mask) is:
TO_DATE(TO_CHAR(date_column, nls_date_format), non_default_format_mask)

Generally, the default nls_date_format parameter is set to dd-MON-yy, so in your query, what is likely to be happening is your date column is converted to a string in the format dd-MON-yy, and you’re then turning it back to a date using the format MMDD. The string is not in this format, so you get an error.

answered Apr 24, 2015 at 16:35

Boneist's user avatar

1

This error can come not only because of the Date conversions

This error can come when we try to pass date whereas varchar is expected
or
when we try to pass varchar whereas date is expected.

Use to_char(sysdate,'YYYY-MM-DD') when varchar is expected

BuZZ-dEE's user avatar

BuZZ-dEE

6,09412 gold badges66 silver badges97 bronze badges

answered Aug 14, 2018 at 16:13

Ankur Bhutani's user avatar

Ankur BhutaniAnkur Bhutani

3,0994 gold badges29 silver badges26 bronze badges

0

I added TO_DATE and it resolved issue.

Before modification — due to below condition i got this error

record_update_dt>='05-May-2017'

After modification — after adding to_date, issue got resolved.

record_update_dt>=to_date('05-May-2017','DD-Mon-YYYY')

Derrick's user avatar

Derrick

3,6795 gold badges35 silver badges50 bronze badges

answered May 22, 2017 at 7:27

karthik G's user avatar

karthik Gkarthik G

4195 silver badges5 bronze badges

ORA-01858

ORA-01858: в дате была обнаружена буква в том месте где должно стоять число

Причина:

Входные данные были преобразованы с использованием картинки формата даты, который был неверен; в картинке формата ожидалось число, но на его месте была обнаружена буква.

Действие:

Проверьте входные данные и картинку формата даты, для того чтобы убедиться, что ее элементы отождествляются в числах, затем повторите операцию.

May 2, 2021

I got ” ORA-01858: a non-numeric character was found where a numeric was expected ” error in Oracle database.

ORA-01858: a non-numeric character was found where a numeric was expected

Details of error are as follows.

ORA-01858: a non-numeric character was found where a numeric was expected

Cause: The input data to be converted using a date format model was incorrect. 
The input data did not contain a number where a number was required by the format model.

Action: Fix the input data or the date format model to make sure the elements match in 
number and type. Then retry the operation.



a non-numeric character was found where a numeric was expected

This ORA-01858 errors are related with the The input data to be converted using a date format model was incorrect.
The input data did not contain a number where a number was required by the format model.

To solve this error, you need to Fix the input data or the date format model to make sure the elements match in
number and type. Then retry the operation.

An example of the this error as follows:

The DATA in SCOTT.EMP is as follows:

EMPNO   ENAME       JOB       MGR        HIREDATE   SAL   COMM    DEPTNO
------- ----------  --------- ---------- ---------  ----- ------- ----------
7900    JAMES       CLERK     7698       03-DEC-81  950           30
7902    FORD        ANALYST   7566       03-DEC-81  3000          20
7934    MILLER      CLERK     7782       23-JAN-82  1300          10
8000    HAMBURGLAR  THIEF     7698       03-MAR-09  100           30
SQL> alter session set NLS_DATE_FORMAT='MM/DD/YYYY';

Session altered.

SQL> alter session set NLS_TIMESTAMP_FORMAT='YYYY-MM-DD-hh24.mi.ss.ff';

Session altered.


SQL> select *
       from scott.emp
      where job='THIEF'
        and TO_DATE(TO_CHAR(HIREDATE, 'DD-MON-YY')) = TO_DATE(TO_CHAR(sysdate-1, 'DD-MON-YY'));

select * from scott.emp where job='THIEF' and TO_DATE(TO_CHAR(HIREDATE, 'DD-MON-YY')) = TO_DATE(TO_CHAR(sysdate-1, 'DD-MON-YY'))
*
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected


SQL> select /*+ rule*/*
       from scott.emp
      where job='THIEF'
        and TO_DATE(TO_CHAR(HIREDATE, 'DD-MON-YY')) = TO_DATE(TO_CHAR(sysdate-1, 'DD-MON-YY'));

select /*+ rule*/* from scott.emp where job='THIEF' and TO_DATE(TO_CHAR(HIREDATE, 'DD-MON-YY')) = TO_DATE(TO_CHAR(sysdate-1, 'DD-MON-YY'))
*
ERROR at line 1:
ORA-01843: not a valid month

Once the NLS Formats are changed back the query now executes without error:

SQL> alter session set NLS_DATE_FORMAT='DD-MON-RR';

Session altered.

SQL> alter session set NLS_TIMESTAMP_FORMAT='DD-MON-RR HH.MI.SSXFF AM';

Session altered.


SQL> select *
       from scott.emp
      where job='THIEF'
        and TO_DATE(TO_CHAR(HIREDATE, 'DD-MON-YY')) = TO_DATE(TO_CHAR(sysdate-1, 'DD-MON-YY'));

no rows selected

SQL> select /*+ rule*/*
       from scott.emp
      where job='THIEF'
        and TO_DATE(TO_CHAR(HIREDATE, 'DD-MON-YY')) = TO_DATE(TO_CHAR(sysdate-1, 'DD-MON-YY'));

no rows selected

You need to read the following post to learn more details about TO_DATE function.

PL/SQL Datetime Functions

Do you want to learn Oracle Database for Beginners, then read the following articles.

Oracle Tutorial | Oracle Database Tutorials for Beginners ( Junior Oracle DBA )

The purpose of this guide is to assist developers in troubleshooting and resolving the ORA-01858 error in Oracle databases. This error occurs when there is a non-numeric character in a numeric field, which affects the proper execution of SQL statements. By following the steps provided in this guide, developers will be able to identify the source of the error and implement a solution to resolve it.

Table of Contents

  1. Understanding the ORA-01858 Error
  2. Identifying the Source of the ORA-01858 Error
  3. Step-by-Step Solution to Resolve the ORA-01858 Error
  4. FAQ
  5. Related Links

Understanding the ORA-01858 Error

The ORA-01858 error occurs when a non-numeric character is found in a numeric field during the execution of an SQL statement. This error typically arises when attempting to insert or update a date value using an incorrect format, leading to a mismatch between the specified format and the actual data.

The error message will look like the following:

ORA-01858: a non-numeric character was found where a numeric was expected

Identifying the Source of the ORA-01858 Error

Before attempting to resolve the error, it is essential to identify the source of the problem. The error message typically provides a clue as to where the issue lies within the SQL statement.

Consider the following example:

INSERT INTO employees (employee_id, hire_date)
VALUES (1, TO_DATE('15-SEP-2021', 'DD-MON-YYYY'));

In this example, the error may occur due to an incorrect date format. The format specified in the TO_DATE function is ‘DD-MON-YYYY’, but the actual date value is in the format ‘DD-MMM-YYYY’.

Step-by-Step Solution to Resolve the ORA-01858 Error

Examine the SQL statement: Review the SQL statement causing the error and identify the date value and its associated format.

Check the date format: Ensure that the date format specified in the SQL statement matches the actual date value. If the formats are different, modify the format accordingly.

Test the modified SQL statement: Execute the modified SQL statement and check if the error persists. If the error still occurs, proceed to the next step.

Verify the column data type: Check the data type of the column in which the date value is being inserted or updated. Ensure that the data type is compatible with the date value.

Update the column data type: If the column data type is found to be incompatible, alter the table to modify the column data type to a compatible one.

Re-execute the SQL statement: After updating the column data type, execute the SQL statement again to verify if the error has been resolved.

FAQ

What is the ORA-01858 error?

The ORA-01858 error occurs when a non-numeric character is found in a numeric field during the execution of an SQL statement in an Oracle database.

What causes the ORA-01858 error?

The ORA-01858 error is typically caused by a mismatch between the specified date format and the actual date value in an SQL statement.

How can I identify the source of the ORA-01858 error?

The error message usually provides a clue as to where the issue lies within the SQL statement. Examine the SQL statement and look for any discrepancies in the date value and its associated format.

Can the ORA-01858 error be caused by an incompatible column data type?

Yes, the ORA-01858 error can also occur if the column data type is incompatible with the date value being inserted or updated.

How can I resolve the ORA-01858 error?

To resolve the ORA-01858 error, ensure that the date format specified in the SQL statement matches the actual date value and that the column data type is compatible with the date value.

  • Oracle Date Functions
  • TO_DATE Function
  • ALTER TABLE Statement

oracle tutorial webinars

ORA-01858

Sometimes in working with database software, matters of syntax and formatting can overlap to create a plethora of problems for users. Some platforms may require only numerical digits in certain sections. Others may need the month in a date written out in full and phonetic form.

Despite the needs of the program, it would be impossible for any one individual to strictly adhere to the parameters of Oracle on every occasion. The ORA-01858 can be considered as a warning to users are jumping between various tables and data sets at a quick pace and are letting the formatting concerns slip through the cracks.

The Problem

The ORA-01858 error is an issue of syntax and formatting. When prompted with the error, the user will receive an accompanying message that will state that “a non-numeric character was located where a digit was expected”. Oracle docs list the cause of this error as “the input data to be converted using a date format model being incorrect. The input data did not contain a number where a number was required by the format model.”

What does this mean in simple terms? This error essentially occurs when a user attempts to convert a string of data into a date, and in doing so specified a date being passed in a particular format. This will most commonly be something along the lines of DD-MM-YYYY. The error pops up when the user then tries to pass the date in the DD-MON-YYYY format. In such a case, a character-stated month such as “JAN” for January or “JUL” for July, will cause the error by inserting an alphabetical name in a place that Oracle expects to read a numerical name (such as “01” for January or “07” for July). Because this error is prompted by a character presence in a numerically-determined field, the most likely culprit will be a date entered in a format that writes the month specifically out in some form.

The Solution

In order to solve this formatting issue, there are basically two options to take. The user can either fix the input data to conform to the format in question, or the user can switch the date format model to ensure that the elements match in number and type and then retry the operation. In most cases, the former will be the simpler strategy.

Before we continue, it would be important to note that if a user is attempting to fetch strings from a table and subsequently convert them into dates, the data in the table should be checked prior to using the date format string. If the table contained strings that are note actually date values and the user attempts to convert them, then this error could be raised (although this is less common).

Since there is no predefined exception for handling this specific error, let us look at an example of a user-defined way of addressing it. In this scenario, the default date format is set to DD-MON-YY and the following statement was executed:

SQL> select to_date(’10-JUN-2014’, ‘DD-MM-YYYY’) from dual;
ERROR:

ORA-01858: a non-numeric character was found where a numeric was expected
no rows selected

At this juncture, after assessing the data and determining that the date information needs to be adjusted, the following can be run:

SQL> select to_date(’10-JUN-2014’, ‘DD-MON-YYYY’) from dual
2 /
TO_DATE( ‘ 10
10-JUN-2014

This should clear up the hypothetical date in question and allow the statement to run smoothly.

Looking forward

Formatting issues can be mentally taxing simply because they can allow a user to second-guess themselves on what information is where. It would be recommended that if you are finding yourself getting frustrated and having to look back to the same information repeatedly, write down the bits that are concrete and a part of your system for reference points. If you are having further concerns over formatting or this error specifically, contacting a licensed Oracle consulting representative can clear up any other database questions.

Понравилась статья? Поделить с друзьями:
  • Ошибка ora 01790
  • Ошибка ora 00922
  • Ошибка lada vesta р0363
  • Ошибка ora 01732
  • Ошибка ora 00920 invalid relational operator