Search code examples
sqloracle-databaseoracle11g

ora-01858 a non numeric charcter was found where a numeric was expected


select *
from   cron_log_razorpay
where   trunc(To_Date(Crtd_Dt, 'DD-MM-YYYY HH24:MI:SS')) = trunc(sysdate)
order by crtd_dt desc;

the data type of crtd_dt is varchar(100), i hot the error fix the input data or the date format model


Solution

  • Fix your table so you store dates as dates.

    ALTER TABLE cron_log_razorpay ADD Crtd_Dt2 DATE;
    
    UPDATE cron_log_razorpay
    SET Crtd_Dt2 = TO_DATE(
                     Crtd_Dt DEFAULT NULL ON CONVERSION ERROR,
                     'DD-MM-YYYY HH24:MI:SS'
                   )
    WHERE Crtd_Dt2 IS NULL;
    

    Then review the columns that failed:

    SELECT *
    FROM   cron_log_razorpay
    WHERE  Crtd_Dt2 IS NULL;
    

    and, if necessary, fix the strings in Crtd_dt and re-run the UPDATE.

    Then you could replace the Crtd_Dt column:

    ALTER TABLE cron_log_razorpay DROP COLUMN Crtd_Dt;
    ALTER TABLE cron_log_razorpay RENAME COLUMN Crtd_Dt2 TO Crtd_Dt;
    

    Then all your values will be valid dates (or NULL values if you have not fixed your data).

    fiddle