Most database servers have a COALESCE function that will return the first argument, which is not 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 the situation, since there seems to be a lot of discussion that COALESCE / ISNULL will still return NULL if the rows do not match, try this query, you can copy and paste into SQL Server directly as it is: / p>
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 rows from sys.columns from consideration, but the sum statement still returns a single row, which is null, which combines the corrections as a single row with 0.
Hope this helps explain this.
Jonathan Rupp Jun 17 '09 at 3:55 2009-06-17 03:55
source share