Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IS NOT NULL for multiple columns

Tags:

sql-server

I want to check for the is not null constraint for multiple columns in a single SQL statement in the WHERE clause, is there a way to do so? Also I don't want want to enforce the NOT NULL type constraint on the column definition.

SELECT * FROM AB_DS_TRANSACTIONS 
WHERE FK_VIOLATION IS NULL 
AND TRANSACTION_ID NOT IN(
    SELECT distinct TRANSACTION_ID FROM AB_TRANSACTIONS) 
AND COUNTRY_ID IS NOT NULL 
AND GEO_CUST_COUNTRY_ID IS NOT NULL 
AND INVOICE_DATE IS NOT NULL 
AND ABB_GLOBALID IS NOT NULL 
AND SALES_ORG_ID IS NOT NULL 
AND DIST_ID IS NOT NULL 
AND CUSTOMER_ID IS NOT NULL 
AND REPORT_UNIT_ID IS NOT NULL 
AND CURR_INVOICE IS NOT NULL 
AND DIVISION_CODE IS NOT NULL 

So instead of using IS NOT NULL again and again I want to simplify things

like image 561
Newbie Avatar asked Feb 29 '16 23:02

Newbie


People also ask

Can we use NOT NULL on multiple columns?

We can add NOT NULL constraint on multiple columns in one table. NOT NULL constraint allows duplicate values. If we defined a column as NOT NULL while inserting or updating values on table, we must and should give value to that specified column. Not Null Constraint allow duplicate values.

How do you add NOT NULL constraints in multiple columns?

"I want to have a not null constraint on all 3 name columns" - simply run "alter table employee modify (<column name> not null)" for each column. 2. Instead of using "NVL(FIRST_NAME,NVL(MIDDLE_NAME,LAST_NAME))", you could just do "COALESCE(FIRST_NAME,MIDDLE_NAME,LAST_NAME)".

How do I check if multiple columns are NULL?

Filtering NULL from Multiple Columns This can be performed with this simple statement using AND with multiple comparison operators: SELECT primary_author, published_date, title FROM books WHERE ( primary_author IS NOT NULL AND published_date IS NOT NULL );


2 Answers

SELECT * FROM AB_DS_TRANSACTIONS 
WHERE COALESCE(COUNTRY_ID,GEO_CUST_COUNTRY_ID,INVOICE_DATE,ABB_GLOBALID,SALES_ORG_ID,DIST_ID,CUSTOMER_ID,REPORT_UNIT_ID,CURR_INVOICE,DIVISION_CODE) IS NOT NULL
like image 175
user3414230 Avatar answered Sep 20 '22 00:09

user3414230


You can use

SELECT * FROM table1 
WHERE NOT (Column1 IS NULL OR 
Column2 IS NULL OR
 Column3 IS NULL OR
 Column4 IS NULL
    IS NOT NULL)

As per OP comment, Updating answer

Inserting Rows by Using INSERT and SELECT Subqueries

INSERT INTO Table_A
    SELECT column1, column2, column3,column4 
    FROM Table_B 
    WHERE NOT (Column1 IS NULL OR 
    Column2 IS NULL OR
    Column3 IS NULL OR
    Column4 IS NULL
    IS NOT NULL);

Your query

I am able to reduce 50 chars approx

SELECT * FROM AB_DS_TRANSACTIONS 
WHERE 
FK_VIOLATION IS NULL 
AND TRANSACTION_ID NOT 
IN(SELECT distinct TRANSACTION_ID FROM AB_TRANSACTIONS) 
AND 
NOT (
COUNTRY_ID IS NULL  
OR GEO_CUST_COUNTRY_ID IS NULL 
OR INVOICE_DATE IS NULL 
OR ABB_GLOBALID IS NULL 
OR SALES_ORG_ID IS NULL 
OR DIST_ID IS NULL 
OR CUSTOMER_ID IS NULL 
OR REPORT_UNIT_ID IS NULL 
OR CURR_INVOICE IS NULL 
OR DIVISION_CODE IS NULL
) 
like image 40
Raju Avatar answered Sep 21 '22 00:09

Raju