Reduce the use of AND operator in Oracle while comparing multiple NULL values in different columns - sql

I want to compare multiple column's NULL values. eg. Assume I have 3 columns in my table from which I have to find out NOT NULL values. I am using following code :
select * from table1 where
column1 is not null
and column2 is not null
and column3 is not null
I don't want to use this code as it uses "and" multiple times if columns goes on increasing.
Anybody have option to this in Oracle 11g ?

I agree with the comment that your query is fine as is. If the columns that you are checking are all of a numeric variety then you can use Oracle's behavior with null values to your advantage to shorten the query like this:
select * from table 1
where
(
column1
+ column2
+ column3
) is not null;
If any of the listed columns are null then the sum will be null also. Unfortunately, if you have strings instead--null strings concatenate just fine, so the same approach doesn't work with them.

You can use
COALESCE (expr1, expr2)
which is equivalent to
CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END
Your syntax would be
coalesce(column1,....,columnn) is not null

You can use this instead of the COALESCE:
SELECT *
FROM table1
WHERE column1 || column2 || column3 || column4 IS NOT NULL;

Tim Rhyne answers well. If you had all string columns, your where clause could be:
WHERE LENGTH(COLUMN1)+LENGTH(COLUMN2)+LENGTH(COLUMN3) IS NOT NULL
If you had a mix of string and numeric:
WHERE COLUMN_INTEGER1+COLUMN_INTEGER2+LENGTH(COLUMN_STRING3) IS NOT NULL

Related

Null query result cant fetch in Select query in Oracle

I have below query which i am trying to run but not returning the expected result. The ISIN field value which is Null in EXPORT_BB is also getting ignore and not showing the result with the condition given in NOT IN clause. The export_blacklist has only one row value and which is not Null but still i dont for what reason the null value is getting ignored.
Select * from EXPORT_BB where ISIN NOT IN
(
SELECT
ISIN
FROM
export_blacklist);
If i run only select query without the NOT IN clause then i can see values which is NULL for ISIN field.
JUst for test i tried below query and its also resulting nothing. Is it bug in Oracle 18c or something is missing?
select 'null is not in set' from dual where null not in (select 1 from dual);
Any comparison of NULL with =, <>, <, > or in a IN or NOT IN clause will return NULL, so that row is not included in the results (because only rows for which the returned value is TRUE will be included in the results).
Change your code with a condition for the case that ISIN is NULL:
SELECT * FROM EXPORT_BB
WHERE ISIN NOT IN (SELECT ISIN FROM export_blacklist)
OR ISIN IS NULL
NULL values doesn't work with NOT IN it's the normal behaviour.
You have to convert the NULL to another value to be able to operate with it or use IS NULL/IS NOT NULL
Select * from EXPORT_BB where NVL(ISIN, 999999) NOT IN
(
SELECT
NVL(ISIN, 999999)
FROM
export_blacklist);
Comparing to a null value in Oracle always returns false.
Is NULL >= 1? No.
Is NULL < 1? No.
Is NULL in your set? Regardless of what your set is, the answer is no.
Is NULL not in your set? Again, no.
It is the expected behaviour. NOt related to 18c it is the same way from Oracle 7 onwards
NOT IN doesnt consider nulls.
NOT EXISTS does consider nulls.
Consider the following example in db fiddle
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=8be0a790d8172093a032602345038e8e
See a discussion on this
https://asktom.oracle.com/pls/apex/asktom.search?tag=in-vs-exists-and-not-in-vs-not-exists
As you have been answered by collegues you have to specify that you wanna return null values too.
Namely
SELECT *
FROM EXPORT_BB
WHERE ISIN NOT IN (SELECT ISIN FROM EXPORT_BLACKLIST)
OR ISIN IS NULL;

How to combine Column1,Column2(Column3) and in the column 3 I had some NULLS. Instead of Nulls I need Column1,Column2

How to comabine Column1,Column2(Column3) and in the column 3 I had some NULLS. Instead of Nulls I need Column1,Column2
Select concat(column1, Column2, Column3) Concatenatedvalue from table1.
I assumed this is SQL server, concat will take care of null values . Please make sure not to use null values because CONCAT_NULL_YIELDS_NULL will be ON as default settings for most of the databases as long as it is not changed.
My database has CONCAT_NULL_YIELDS_NULL ON feature.
SELECT 'abc' + NULL fromplus , concat('ABC', Null) fromconcat ;
Output:
fromplus fromconcat
NULL ABC
If you see I got null from '+' and valid value from concat.

remove all NULL valued rows from table?

My sql query working fine but i have another issue some rows in my table have NULL values. i want to remove all NULL valued rows. Any recommendations?
Delete statement should work
DELETE FROM your_table
WHERE your_column IS NULL;
In case of multi column NULL check, I suggest using COALESCE
DELETE FROM your_table
WHERE COALESCE (your_column1, your_column2, your_column3 ) IS NULL;
If you want to delete a row where all columns values are null then use following query to delete:
DELETE FROM your_table
where your_column1 IS NULL
AND your_column2 IS NULL
AND your_column3 IS NUL;
But if you want to delete a row where any column value is null then use following query to delete:
DELETE FROM your_table
where your_column1 IS NULL
OR your_column2 IS NULL
OR your_column3 IS NUL;
You could start by filtering out columns you don't need, want, or will not use.
SELECT column1, column2, column3
FROM table
Then to remove the null values from your queries, use the IS NOT NULL command. Remember, null values can also be zeros, depending on how your tables and the data formats have been set up. In the example below, assume column2 is a number format.
WHERE column1 IS NOT NULL AND column2 > 0 AND column3 IS NOT NULL

Comparing all data in two tables whilst ignoring certain differences

I am currently trying to compare two SQL server database tables. I've found various methods online, some which seem to work, other which don't.
The one I used which worked was:
select * from table1
except
select * from table2
The only issue with this then (as far as I am aware) is, the table says there is a difference between a 'NULL' value and a '0'. Which is correct, there is a difference.
However my question is, is there a way to do the comparison difference check, whilst ignoring certain conditions such as NULL and 0.
You can use
select isnull(column1, 0) as column1 from table1
except
select isnull(column1, 0) as column1 from table2
to consider the values 0 and null as same.
Also, If you wish to consider more values as same like null = 0 = '' (empty string)
You can use case:
select case when (column1 is null or column1 = '') then 0 end as column1 from table1
except
select case when (column1 is null or column1 = '') then 0 end as column1 from table2

Select rows where column is null

How do you write a SELECT statement that only returns rows where the value for a certain column is null?
Do you mean something like:
SELECT COLUMN1, COLUMN2 FROM MY_TABLE WHERE COLUMN1 = 'Value' OR COLUMN1 IS NULL
?
I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:
SELECT * FROM customers WHERE first_name IS NULL
On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:
SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23
Other DBMSes have similar functionality available.
If you want to know whether a column can be null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.
Use Is Null
select * from tblName where clmnName is null
You want to know if the column is null
select * from foo where bar is null
If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it
does not work:
select * from foo where bar <> 'value'
does work:
select * from foo where bar <> 'value' or bar is null
in Oracle (don't know on other DBMS) some people use this
select * from foo where NVL(bar,'n/a') <> 'value'
if I read the answer from tdammers correctly then in MS SQL Server this is like that
select * from foo where ISNULL(bar,'n/a') <> 'value'
in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.
select Column from Table where Column is null;
select * from tableName where columnName is null
For some reasons IS NULL may not work with some column data type. I was in need to get all the employees that their English full name is missing, I've used:
SELECT emp_id, Full_Name_Ar, Full_Name_En
FROM employees
WHERE Full_Name_En = '' or Full_Name_En is null