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;
To fix the error, specify a month value that is valid such as “January”. Some conversion is built-in, and a value of “Jan” is also valid for the Month format code.
RR is an "input" format. it means if you enter to_date( '01-jan-40', 'dd-mon-rr' ) Oracle will slide around the date based on the current year. In 1999 and 2001 -- that would be the year 2040. As opposed to yy -- where the century is based on the current date. meaning.
The TO_DATE function accepts an argument of a character data type and converts this value to a DATETIME value. The TO_DATE function evaluates a character string according to the date-formatting directive that you specify and returns a DATETIME value.
SYSDATE returns the current date and time set for the operating system on which the database resides. The datatype of the returned value is DATE , and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter. The function requires no arguments.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With