Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL minus 2 columns - with null values

Tags:

sql

I have this table (made from a SQL query):

Row 1 Row 2 
2     1    
3     NULL 

And I want to minus the 2 columns, so I just select like this:

Select Row1 - Row2
From table

But then I get this result:

1
NULL

instead of:

1
3

How can I make it possible to get the last result?

like image 593
MMM Avatar asked Apr 08 '13 10:04

MMM


People also ask

How do I subtract two records in SQL?

The Minus Operator in SQL is used with two SELECT statements. The MINUS operator is used to subtract the result set obtained by first SELECT query from the result set obtained by second SELECT query.

What can I use instead of MINUS in SQL?

Minus Query TIP: The MINUS operator is not supported in all SQL databases. It can used in databases such as Oracle. For databases such as SQL Server, PostgreSQL, and SQLite, use the EXCEPT operator to perform this type of query.

Can we compare two NULL values in SQL?

SQL has the is [not] null predicate to test if a particular value is null . With is [not] distinct from SQL also provides a comparison operator that treats two null values as the same. Note that you have to use the negated form with not to arrive at similar logic to the equals ( = ) operator.

Can you sum nulls in SQL?

MySQL and PostgreSQL cannot sum up NULL values with the + value. The sum value will be NULL . If you want to do additions in the database: use SUM if it's an option to sum up a column of a result set instead of expressions ( SUM ignores NULL values)


1 Answers

Please try:

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

For more Information visit ISNULL

like image 87
TechDo Avatar answered Sep 30 '22 22:09

TechDo