Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using Sum in sql statements

Author  Topic 

jept
Starting Member

14 Posts

Posted - 2007-05-21 : 17:46:51
I have written an sql statement thats using sum. The problem that I have is that when the value is zero it returns Null. Is there a way that I could return zero instead of null.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-21 : 18:22:49
isnull(value, 0)
or
coalesce(value, 0)


KH

Go to Top of Page

jept
Starting Member

14 Posts

Posted - 2007-05-22 : 10:59:52
quote:
Originally posted by khtan

isnull(value, 0)
or
coalesce(value, 0)


KH





This is my statement:
Select Sum(isnull(Weight, 0)) as total From Table where Active ='Y'
I tried your suggestion and I still get null returned. I just want to know if I am writting the statement right.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-22 : 11:12:23
If there are no rows matching the criteria in WHERE clause, SUM() will return NULL as well.

Try this:

Select Coalesce(Sum(Coalesce(Weight, 0)), 0) as total From Table where Active ='Y'



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

jept
Starting Member

14 Posts

Posted - 2007-05-22 : 11:36:34
the statement you gave me works TY
Go to Top of Page

subrata4allfriends
Starting Member

24 Posts

Posted - 2007-05-22 : 13:42:33
You can try in this way also .....

SELECT ISNULL(SUM(Weight), 0) AS TOTAL FROM Table WHERE Active ='Y'
Go to Top of Page
   

- Advertisement -