SQL query

Status
Not open for further replies.

MrCatbert

Ars Tribunus Angusticlavius
9,107
Subscriptor
I'm trying to create a stored procedure to get the sums of values in a column, broken down by month over a year. What I've got looks like this:

select sum(n_colinches) from tbl_articles where dt_date between '09/01/2001' and '09/30/2001'
union
select sum(n_colinches) from tbl_articles where dt_date between '10/01/2001' and '10/31/2001'
union
"
"
select sum(n_colinches) from tbl_articles where dt_date between '08/01/2002' and '08/31/2002'

Of course the problem is that it returns NULL values for date periods that don't exist yet, so in this example it's only returning a couple of rows. Is there a better way to do this, or at least a way to fix it where it returns rows for each month even if the value would be NULL for that month?
 

kleel

Ars Scholae Palatinae
1,022
try using ISNULL(check_expression, replacement_value)
Arguments
check_expression
Is the expression to be checked for NULL. check_expression can be of any type.
replacement_value
Is the expression to be returned if check_expression is NULL. replacement_value must have the same type as check_expresssion.
Return Types
Returns the same type as check_expression.

Remarks
The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned.


e.g.

select sum(ISNULL(n_colinches,0)) from tbl_articles where dt_date between '09/01/2001' and '09/30/2001'

HTH

Kleel
 
Status
Not open for further replies.