Casting in case statement

General
Enthusiast

Casting in case statement

Hi ,

I am trying to cast date to default values inside the case statement ,

 Select

CAST (CASE WHEN OFR_STRT_DT='1900-01-01' THEN ‘NA’ ELSE OFR_STRT_DT END as varchar (10) ) as OFR_STRT_DT

From UD466.FF_PRCSS

Where elg_flg = 'Y';

Datatype of OFR_STRT_DT is date but while converting to default value 'NA' its not working , it says datatype mismatch

2 REPLIES
Senior Apprentice

Re: Casting in case statement

You need to cast only the date not the case:

CASE WHEN OFR_STRT_DT='1900-01-01' THEN ‘NA’ ELSE CAST (OFR_STRT_DT as varchar (10) ) END as OFR_STRT_DT

Dieter

Enthusiast

Re: Casting in case statement

Thank you so much Dieter !!