简体   繁体   中英

Convert String to Date in CASE expression Oracle SQL

I'm using the following CASE expression, but I want the field to be a Date and not string format:

CASE WHEN TO_CHAR(DATE, 'MM/DD/YYYY') = '01/01/1800' THEN '01/01/2029'  
         ELSE TO_CHAR(DATE, 'MM/DD/YYYY')
     END as ENDDATE,

I tried adding in TO_DATE( before the TO_CHAR , but I keep getting an error on the Then part. Any pointers to how to successfully add in TO_DATE to this statement?

You could use date literal (which is always yyyy-mm-dd ) and compare it directly to your date_column :

case when trunc(date_column) = date '1800-01-01' then date '2029-01-01'
     else date_column
end as enddate
select 
case when date_column=to_date('01/01/1800','dd/mm/yyyy') then to_date('01/01/2029','dd/mm/yyyy')
     else date_column end as enddate
from dual;

You just have to make sure all the possible outcomes of the case statement belong to the same data type.

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