Question

I have a Case-When clause like this;

(CASE WHEN A.YAZ_ADRES IS NULL
         THEN (B.IS_ADRES1 +' '+B.IS_ADRES2) 
         ELSE A.YAZ_ADRES END) 

I want to use a condition after THEN

For Example;

(CASE WHEN A.YAZ_ADRES IS NULL
         THEN (IF B.TUZ = 'T' THEN (B.IS_ADRES1 +' '+B.IS_ADRES2) ELSE ((B.EV_ADRES1 +' '+B.EV_ADRES2)))
         ELSE A.YAZ_ADRES END) 

How can I use nested condition in Case-When ?

Was it helpful?

Solution

You can nest CASE clauses like this:

(CASE WHEN A.YAZ_ADRES IS NULL
  THEN
    (CASE WHEN B.TUZ = 'T'
      THEN (B.IS_ADRES1 +' '+B.IS_ADRES2)
      ELSE ((B.EV_ADRES1 +' '+B.EV_ADRES2))
    END)
  ELSE A.YAZ_ADRES
END)

OTHER TIPS

Nest another CASE instead of an IF

You can use CASE WHEN ... THEN ... (WHEN ... THEN ...)+ ELSE ... END. It returns the THEN expression of the first matching WHEN condition:

CASE WHEN A.YAZ_ADRES IS NULL AND B.TUZ = 'T'
       THEN B.IS_ADRES1 +' '+B.IS_ADRES2
     WHEN A.YAZ_ADRES IS NULL 
       THEN B.EV_ADRES1 +' '+B.EV_ADRES2
     ELSE A.YAZ_ADRES
END
(CASE WHEN A.YAZ_ADRES IS NULL          
  THEN (CASE WHEN B.TUZ = 'T' THEN (B.IS_ADRES1 +' '+B.IS_ADRES2) ELSE ((B.EV_ADRES1 +' '+B.EV_ADRES2)) END)          
ELSE A.YAZ_ADRES END)  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top