SQL SERVER – Puzzle Involving NULL – Resolve – Error – Operand data type void type is invalid for sum operator
Posted
by pinaldave
on SQL Authority
See other posts from SQL Authority
or by pinaldave
Published on Mon, 27 Jun 2011 01:30:04 +0000
Indexed on
2011/06/27
8:26 UTC
Read the original article
Hit count: 416
Pinal Dave
|PostADay
|sql
|SQL Authority
|SQL Puzzle
|SQL Query
|SQL Scripts
|SQL Server
|SQL Tips and Tricks
|SQLServer
|T SQL
|Technology
Today is Monday let us start this week with interesting puzzle. Yesterday I had also posted quick question here: SQL SERVER – T-SQL Scripts to Find Maximum between Two Numbers
Run following code:
SELECT SUM(data)
FROM (SELECT NULL AS DATA) t
It will throw following error.
Msg 8117, Level 16, State 1, Line 1
Operand data type void type is invalid for sum operator.
I can easily fix if I use ISNULL Function as displayed following.
SELECT SUM(data)
FROM (SELECT ISNULL(NULL,0) AS DATA) t
Above script will not throw an error. However, there is one more method how this can be fixed.
Can you come up with another method which will not generate error?
Reference: Pinal Dave (http://blog.SQLAuthority.com)
Filed under: Pinal Dave, PostADay, SQL, SQL Authority, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology
© SQL Authority or respective owner