SQL minus 2 columns - with null values

Please try:

SELECT ISNULL([Row 1], 0) - ISNULL([Row 2], 0) from YourTable

For more Information visit ISNULL


The MySQL equivalent of ISNULL is IFNULL

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

Maybe also look at SQL NULL Functions

The ISNULL from MySQL is used to check if a value is null

If expr is NULL, ISNULL() returns 1, otherwise it returns 0.


The reason you got this is because Any Mathematical operation with NULL produces NULL So while doing operation all values should be read as NULL=0.

With ISNULL()

Hence

SELECT ISNULL([Row 1], 0) - ISNULL([Row 2], 0) from YourTable

in sql anything minus with NULL then it is always NULL so you need to convert NULL to Zero

SELECT ISNULL(ROW1,0)-ISNULL(ROW2,0) FROM YOUR_TABLE

Tags:

Sql