Can we use IF ELSE condition inside CASE statement like below format
case when DATENAME(SECOND, GETDATE()) IN (N'Saturday', N'Sunday')
then if () then
else if then
else
end,
else
'Weekday'
end
Can we use IF ELSE condition inside CASE statement like below format
case when DATENAME(SECOND, GETDATE()) IN (N'Saturday', N'Sunday')
then if () then
else if then
else
end,
else
'Weekday'
end
We can nest CASE expressions, or use multiple tests if appropriate.
These 2 example give the same result.
select
x,y,
case
when x = 1 then
case when y = 1 then 11
else 12
end
when x = 2 then
case when y = 1 then 21
else 22
end
else 99 end myExression
from test;
| x | y | myexression |
|---|---|---|
| 1 | 2 | 12 |
| 1 | 1 | 11 |
| 2 | 1 | 21 |
| 2 | 2 | 22 |
| null | null | 99 |
select
x,y,
case
when x = 1 and y = 1 then 11
when x = 1 and y = 2 then 12
when x = 2 and y = 1 then 21
when x = 2 and y = 2 then 22
else 99
end myExpression
from test;
| x | y | myexpression |
|---|---|---|
| 1 | 2 | 12 |
| 1 | 1 | 11 |
| 2 | 1 | 21 |
| 2 | 2 | 22 |
| null | null | 99 |
db<>fiddle here