January 14, 2012

Aggregate function ignore Null values

Aggregate functions ignore Null values in SQL Server.

DECLARE @Table TABLE
(Col1 VARCHAR(10),Col2 INT)

INSERT INTO @Table
SELECT 'C1',10   UNION ALL
SELECT 'C2',20   UNION ALL
SELECT 'C3',30   UNION ALL
SELECT 'C4',Null UNION ALL
SELECT 'C5',Null

SELECT AVG(Col2) AS 'Average' FROM @Table

image

Because it will ignore last two Null values.

That’s It.
Enjoy learning.

No comments:

Post a Comment