tags:

views:

2210

answers:

5

Hi,

I have a query that counts the price of all items between two dates. Here is the select statement:

SELECT SUM(Price) AS TotalPrice
FROM Inventory
WHERE (DateAdded BETWEEN @StartDate AND @EndDate)

You can assume all of the tables have been set up properly.

If I do a select between two dates and there are no items within that date range, the function returns NULL as the TotalPrice rather than 0.

How can I make sure that if no records are found, 0 gets returned rather than NULL?

Thanks,
Matt

+8  A: 

Most database servers have a COALESCE function, which will return the first argument that is non-null, so the following should do what you want:

SELECT COALESCE(SUM(Price),0) AS TotalPrice
FROM Inventory
WHERE (DateAdded BETWEEN @StartDate AND @EndDate)

[edit]

Just to clarify things since there seems to be a lot of discussion about "COALESCE/ISNULL will still return NULL if no rows match", try this query you can copy-and-paste into SQL Server directly as-is:

SELECT coalesce(SUM(column_id),0) AS TotalPrice 
FROM sys.columns
WHERE (object_id BETWEEN -1 AND -2)

Note that the where clause excludes all the rows from sys.columns from consideration, but the 'sum' operator still results in a single row being returned that is null, which coalesce fixes to be a single row with a 0.

I hope this helps explain it.

Jonathan
If you read the question he wants 0 to be returned if there were no results, this will only work if the result of the sum is null
Tetraneutron
@Tetraneutron: I think it will work. sum(price) will be null if there are no rows, but there will always be exactly one result row.
Thilo
COALESCE works fine so long as you're getting a row back for it to operate on (which you do in this case). COALESCE won't help you if you get no rows back though.
Jonathan
I guess I should add that I only tried this on SQL Server 2008 and MySQL 5-- I don't have any other DBs available, but I'm pretty certain that isnull/coalesce alone will do it on any DB.
Jonathan
You don't have a row to operate on, the question states "no records are found", so no row to operate on, so coalesce won't work.
Tetraneutron
@Tetraneutron -- try it on a real database -- no records match the where clause, but summing zero records results in a single row with a 'null' value.
Jonathan
Actually yes you are right, I copied your answer across to test it, but in the process of fitting it to a temp table must have dropped the "Sum" off resulting in no rows returned, My apologies with an upvote.
Tetraneutron
+2  A: 
SELECT ISNULL(SUM(Price), 0) AS TotalPrice 
FROM Inventory
WHERE (DateAdded BETWEEN @StartDate AND @EndDate)

That should do the trick.

Joseph
+1  A: 

Edit: Looks like everyone else beat me to it haha

Found the answer.

ISNULL() determines what to do when you have a null value. In this case my function returns a null value so I needed specify a 0 to be returned instead.

SELECT ISNULL(SUM(Price), 0) AS TotalPrice FROM Inventory WHERE (DateAdded BETWEEN @StartDate AND @EndDate)

Matt
isnull or coalesce will work
BlackTigerX
I believe IFNULL is the MySQL equivalent, and ISNULL is for MS' T-SQL.
ojrac
You have the ISNULL statment backwards for what you are trying to do. I think you want this instead: SUM(ISNULL(Price,0))
JohnFx
+6  A: 
SELECT 0+COALESCE(SUM(Price),0) AS TotalPrice
FROM Inventory
WHERE (DateAdded BETWEEN @StartDate AND @EndDate)
BlackTigerX
This will do it
Tetraneutron
I just tested it with an empty table and it works RBarry...
Telos
See my answer below. Just use ISNULL (or COALESCE) twice, once for each row, then for the sum. SELECT ISNULL(SUM(ISNULL(Price, 0)), 0))
le dorfier
+1  A: 

USE

SELECT ISNULL(SUM(ISNULL(Price, 0)), 0). I'm 99% sure that will work.

le dorfier