16.8.08

Null value handling in Count function

Let's see some interesting facts of NULL value in SQL Server.Am trying to Insert some Null values in my table and check the count of the records available in my table.

Code Snippet
DECLARE @t table(
id int identity(1,1), value varchar(50))
INSERT INTO @t values(NULL)
INSERT INTO @t values(NULL)
INSERT INTO @t values(NULL)
INSERT INTO @t values('1')
INSERT INTO @t values('2')
INSERT INTO @t values('3')
SELECT count(id), count(value) FROM @t

with out executing the code, Can you tell me the output?

OOps, Its 6,3.... How? Its because, SQL Server will discard the null values and considered only the valid values for count function.

Happy Learning!!!

Regards,

Venkatesan Prabu .J

No comments:

Post a Comment