Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ISNULL(SUM(x),0) OR SUM(ISNULL(x,0) in SQL server

Which one of the following is correct?

        SUM(ISNULL(Sales,0)) AS Sales,
        ISNULL(SUM(Sales),0) AS Sales,

Or are they both correct?

like image 935
Alborz Avatar asked May 01 '13 16:05

Alborz


People also ask

How do I use Isnull with sum in SQL?

SELECT SUM(ISNULL(Salary, 10000) AS Salary FROM Employee; Output: In MySQL, ISNULL() function is used to test whether an expression is NULL or not. If the expression is NULL it returns TRUE, else FALSE.

How do you use Isnull and sum?

You wrap the SUM with an ISNULL, ISNULL(Sum(Ac_Billbook. CostsNet), 0) because SUM ignores nulls when summing or you can reverse the SUM and ISNULL like this, Sum(ISNULL(Ac_Billbook. CostsNet, 0)) which will convert individual NULL's to 0. Doesn't really matter which one you do.

What is the difference between Isnull and is NULL in SQL?

Example 4: Difference between SQL Server ISNULL with IS NULL You might confuse between SQL Server ISNULL and IS NULL. We use IS NULL to identify NULL values in a table. For example, if we want to identify records in the employee table with NULL values in the Salary column, we can use IS NULL in where clause.

Does sum ignore NULL values?

SUM can be used with numeric columns only. Null values are ignored.


3 Answers

They both return the same except if you are running a query on an empty result set.

WITH Sales(Sales) AS
(
SELECT 1
)
SELECT
        SUM(ISNULL(Sales,0)) AS Sales,
        ISNULL(SUM(Sales),0) AS Sales
FROM     Sales    
WHERE 1=0

Returns

Sales       Sales
----------- -----------
NULL        0

The SUM(ISNULL(Sales,0)) version would avoid the ANSI WARNINGS about aggregating NULL.

One other subtle difference is that the datatype of the result column of ISNULL(SUM(Sales),0) is not regarded as nullable.

like image 105
Martin Smith Avatar answered Oct 21 '22 21:10

Martin Smith


The first one says

SUM up every Sales field, and if the field is NULL, treat it as zero.

The second says Sum up the sales field, and if the total is NULL, report a zero instead...

However, the SUM() command skips NULL (although you'll be warned about it), so the first one will cause you not to get the error message

like image 34
Sparky Avatar answered Oct 21 '22 22:10

Sparky


Try this:

DECLARE @table TABLE
(
  id INT IDENTITY,
  Alborz INT
)
INSERT  INTO @table
        SELECT  1
        UNION ALL
        SELECT  1
        UNION ALL
        SELECT  1
        UNION ALL
        SELECT  1
        UNION ALL
        SELECT  1
        UNION ALL
        SELECT  NULL

SELECT  ISNULL(SUM(Alborz), 0)
FROM    @table
SELECT  SUM(ISNULL(Alborz, 0))
FROM    @table

DELETE  FROM @table

SELECT  ISNULL(SUM(Alborz), 0)
FROM    @table
SELECT  SUM(ISNULL(Alborz, 0))
FROM    @table

You'll get 21, 21, 0, and NULL. If you don't want to handle nulls, then ISNULL(SUM(X),0) is the way to go.

like image 4
SQLMason Avatar answered Oct 21 '22 22:10

SQLMason