Forgive me as I am new to this. I am trying to add a year to a date in a query. The year to be added is based on the month/day in the database. If the date is prior to November 1st then the year will be 2017 if it is after November 1st then it will be 2018. I have tried this a few ways (see below) and can get the years added in a query but when I put them in a case statement I get the 'Invalid Number' error.
Using to_char on the date:
CASE
WHEN to_char(au.creat_ts, 'MMDD') >= to_char('11/01/2018', 'MMDD') THEN
to_char(to_date( '2017'||to_char(au.creat_ts,'MMDDHH24MISS'),
'YYYYMMDDHH24MISS' ), 'MM/DD/YYYY')
ELSE
to_char(to_date( '2018'||to_char(au.creat_ts,'MMDDHH24MISS'), 'YYYYMMDDHH24MISS' ), 'MM/DD/YYYY')
END cmpltn_dt,
Adding Months:
CASE
WHEN to_char(au.creat_ts, 'MMDD') >= to_char('11/01/2018', 'MMDD') THEN
trunc(add_months(au.creat_ts,
floor(months_between(SYSDATE, au.creat_ts) / 12) * 12)) --calcx --add years
ELSE
trunc(add_months(au.creat_ts,
(floor(months_between(SYSDATE, au.creat_ts) / 12) - 1) * 12))
END calcx,
These run in select from dual statements with no error. Any ideas? Thanks so much in advance.
If
AU.CREAT_TSisDATE, this works OK (meaning: it doesn't fail):Note the difference: you used
and it raises the error because
'11/01/2018'is a string; it is not a date. If you want to use a date, you have to tell Oracle so. How? See my working example.