Regarding CASE expression in SELECT clause
02:01 16 Sep 2013

What is best approach

Approach-I

SELECT 
SUM(CASE WHEN CODE = 'A' THEN ISNULL(UNIT,0.00)+ISNULL(UNIT_1,0.00) END) AS UNIT_SUM
FROM tblA

Approach-II

SELECT 
ISNULL(SUM(CASE WHEN CODE = 'A' THEN UNIT+UNIT_1 END),0.00) AS UNIT_SUM
FROM tblA
  1. My concern is do I have to place ISNULL inside the CASE expression OR outside the CASE expression. Does it affect summation of units OR both query give same result. And what happen if any UNIT column have NULL value. Does it result NULL of total.

  2. Must I use ELSE in the CASE expression as given below:

THEN ... **ELSE 0.00** END
sql sql-server t-sql