SQL: Nested Condition in Case When Clause

☆樱花仙子☆ 提交于 2019-12-25 03:46:14

问题


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 ?


回答1:


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)



回答2:


Nest another CASE instead of an IF




回答3:


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



回答4:


(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)  


来源:https://stackoverflow.com/questions/7305176/sql-nested-condition-in-case-when-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!