So basically I’m making a program in C# and I need to make a query to the database. In this query I need to sum up all the values of a given column granted 2 other columns match my requirements. Then I take the value and put it into an “int” type variable.
SELECT SUM(Column1) FROM table WHERE Column2="blahblah" AND Column3="Case1" ...
int the_sum = System.Convert.ToInt32(sql.ExecuteScalar());
Now the problems is, Column3 can only have 2 types of string values, here I will call them Case1 or Case2 and I need the SUM of Column1 ONLY when column3 contains the Case1 value. Everything’s cool, except that its possible that all the values in Column3 are Case2. That way, there will be no instances for summing up and an error appears in the second line where I want to put the sum in my “the_sum” int:
int the_sum = System.Convert.ToInt32(sql.ExecuteScalar());
How do I get by this? Am I doing it all wrong here?
Change the
SUM(column1)toISNULL(SUM(column1),0)This way you will get back zero when there are no values to add..