Klapsius
Klapsius

Reputation: 3359

sql server ISNULL not working for my query

This is my second question about this problem. I would like get sum of column roll_sum script:

($sum_number + (SELECT SUM(roll_sum) FROM table_name))

not work because collumn ROLL_SUM is NULL. But if try use replacement:

($sum_number + (SELECT SUM(ISNULL(roll_sum, 0)) FROM table_name))

not work aswell. But second script should replace NULL to 0?

Upvotes: 1

Views: 93

Answers (2)

Roger Wolf
Roger Wolf

Reputation: 7712

Swap ISNULL() with SUM():

SELECT isnull(SUM(roll_sum), 0) FROM table_name;

Upvotes: 1

Junyoung LEE
Junyoung LEE

Reputation: 107

I think there is no problem in your sql query but, you are calling sql query in php statement directly. The below is sample code. please refer to it.

$db = mysql_connect("hostname", "username", "password");
mysql_select_db("dbname", $db) or die("connection failed");

$query = mysql_query("select sum(roll_sum) as sum from table_name", $db);
$query_row = mysql_fetch_array($query);

// to do something you want
$value = $sum_number + $query_row["sum"];

mysql_close($db);

Upvotes: 0

Related Questions