问题
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