Case the when (* * *) then (* * *) end
Can you tell me what's the difference between these two statements?
CodePudding user response:
USE tempdb for
GO
IF OBJECT_ID (' t ') IS NOT NULL
DROP TABLE t
GO
CREATE TABLE (t
N INT
)
GO
INSERT INTO t VALUES (n) (1), (2), (3);
- above for testing table and test data -- -- -- -- -- --
SELECT
The CASE WHEN n> 2 THEN 2 ELSE 0 END AS s1,
The CASE WHEN n> THEN 2 END AS s2,
N
The FROM t
- the following result is obvious:
- if not ELSE, also is not to the specified value, the result is NULL
/*
S1 s2 n
0 NULL 1
0 NULL 2
2, 2, 3,
*/