简体   繁体   中英

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

I am beginner in oracle and need help regarding following query:

select admin_id, to_date(date_created, 'DD-MM-YYYY'), name, mobile, email, (select status from status where status.status_id = admin.status_id) from admin;

Here are my tables:

SQL> desc admin
 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ADMIN_ID                  NOT NULL NUMBER(5)
 STATUS_ID                 NOT NULL NUMBER(5)
 DATE_CREATED                       TIMESTAMP(6)
 STATUS_DATE                        DATE
 NAME                           VARCHAR2(45)
 MOBILE                         VARCHAR2(20)
 EMAIL                          VARCHAR2(110)

SQL> desc status
 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 STATUS_ID                 NOT NULL NUMBER(5)
 STATUS                         VARCHAR2(36)

The main problem is I want to get status.status on basis of admin.status_id. The date could be fixed by using it as char to_char(date_created,'DD/MM/YYYY')

to_date(date_created, 'DD-MM-YYYY') doesn't make a whole lot of sense.

  • date_created is a timestamp with 6 decimal digits of sub-second precision.
  • to_date takes a string. So Oracle has to first convert the timestamp to a string using the session's nls_timestamp_format . That is the string that gets sent to to_date .
  • to_date then tries to convert that string back to a date (which will always have a time component) using the "DD-MM-YYYY" format mask. Unless that happens to be what your nls_timestamp_format is set to, that conversion will fail and will probably throw the error you reported.

If all this casting worked, you'd end up sending the Java application a date that has the time set to midnight. As a general rule, though, it would normally make more sense to just send the timestamp back to the Java application and let it handle how to convert that to a string and what components of the timestamp needed to be shown. So my default would be to just select date_created .

If you really want Oracle to do the data type conversion and to set the time component of the date to midnight, you can use trunc and cast instead. This avoids implicit data type conversion and eliminates dependencies on the session's nls_timestamp_format .

trunc( cast( date_created as date ) )
  1. Check the values in the admin_id and status_id columns. Make sure they are correct data types.

  2. Can you rearrange the query below and check output. Try to use join where possible instead of the nested query to improve the performance of the database:

     select ad.admin_id, to_date(ad.date_created, 'DD-MM-YYYY'), ad.name, ad.mobile, ad.email,st.status from admin ad,status st where ad.admin_id=st.status_id;
  3. Here is link for a similar issue: Getting Error - ORA-01858: a non-numeric character was found where a numeric was expected (Stackoverflow)

ORA-01858: a non-numeric character was found where a numeric was expected
select ad.admin_id, to_date(ad.date_created, 'DD-MM-YYYY'), 
       ad.name, ad.mobile, ad.email,st.status  
from admin ad,status st 
where ad.admin_id=st.status_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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