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
)
Answer from Raju on Stack OverflowYou 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
)
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
Videos
With De Morgan's law:
NOT (A OR B) = (NOT A) AND (NOT B)
you save 20 chars ;)
NOT (
weight IS NULL OR
weight_unit IS NULL OR
length IS NULL OR
width IS NULL OR
height IS NULL OR
dimensional_unit IS NULL
)
As far as I know, there isn't such a syntax.
But if all of them are numeric you can use this trick:
weight + weight_unit + length + width + height + dimensional_unit is not null
An extension to @db2's answer with less (read:zero) hand-wrangling:
DECLARE @tb nvarchar(512) = N'dbo.[table]';
DECLARE @sql nvarchar(max) = N'SELECT * FROM ' + @tb
+ N' WHERE 1 = 0';
SELECT @sql += N' OR ' + QUOTENAME(name) + N' IS NULL'
FROM sys.columns
WHERE [object_id] = OBJECT_ID(@tb)
AND is_nullable = 1;
EXEC sys.sp_executesql @sql;
You should list out all the columns as per JNK's comment.
WHERE c1 IS NULL OR c2 IS NULL OR c3 IS NULL
A somewhat less efficient approach that avoids this is below though.
;WITH xmlnamespaces('http://www.w3.org/2001/XMLSchema-instance' AS ns)
SELECT *
FROM YourTable AS T1
WHERE (
SELECT T1.*
FOR XML PATH('row'), ELEMENTS XSINIL, TYPE
).exist('//*/@ns:nil') = 1
(Based on this SO answer)
No, you need to apply IFNULL to each column individually.
However, if you had a slightly different requirement, that is, to show the first non-null column from a number of columns, and show a default if they are all null, you could use the COALESCE function like so:
select coalesce(col1,col2,col3,0)
from tbl
The problem with this is that it will return a single column in the result, and not multiple columns corresponding to col1, col2 and col3. Therefore, as long as you want to have multiple columns in your result set, you need to do the null handling on a per-column basis.
No, there's no way to use IFNULL on multiple columns at once, you have to do it on each column separately:
SELECT a.id, a.somecol, IFNULL(b.col1, 0) col1, IFNULL(b.col2, "") col2, ...
FROM Table1 AS a
LEFT JOIN Table2 AS b ON <some condition>
The screenshot of your data and output clearly shows the problem. The concatenation operator automatically takes care of NULL and you don't need to do anyhting. But you don't have NULL in your tables, you have 'NULL', a string, in your tables.
SQL> select 'A' || ' ' || null || ' ' || 'B' from dual;
'A'|
----
A B
SQL> select 'A' || ' ' || 'NULL' || ' ' || 'B' from dual;
'A'||''|
--------
A NULL B
If you really had NULL in your table, SQL Developer would display (null) in the data grid.
Clean your data, then try again.
update ap.vendors set vendor_address2 = null where vendor_address2 = 'NULL';
Alternatively, you can turn string 'NULL' to a real NULL on the fly:
SELECT vendor_name,
vendor_address1 || ' ' || decode(vendor_address2, 'NULL', null, vendor_address2) || ' ' || vendor_city || ', ' || vendor_state || ' ' || vendor_zip_code AS "Complete Address"
FROM ap.vendors
ORDER BY vendor_name;
But don't do that. Take care of your data.
The WHERE clause will only exclude full rows, not individual columns. For dealing with individual columns, you need to use something like NVL or Case expressions.
I don't have an Oracle instance to test against right now, but something strikes me as odd in your data. Nulls in Oracle are supposed to concatenate to empty strings. Is it possible that your vendor_address2 column actually contains the string 'NULL' instead of a null value?
You can use a NOT EXISTS:
SELECT DISTINCT T.Title
, T.Value
FROM mytable T
WHERE T.Value IS NOT NULL
OR NOT EXISTS (
SELECT NULL
FROM mytable T2
WHERE T2.Value IS NOT NULL
AND T2.Title = T1.Title
)
You can avoid using the unions if you want and try this:
DECLARE @myTable AS TABLE (Title CHAR(4) NOT NULL, Value INT NULL);
INSERT INTO @myTable (Title, Value)
VALUES ('ex1', 8)
, ('ex1', 9)
, ('ex1', NULL)
, ('ex2', 8)
, ('ex2', NULL)
, ('ex3', NULL);
SELECT DISTINCT
T1.Title
, T2.Value
FROM @myTable T1
LEFT JOIN @myTable T2 ON T2.Title = T1.Title
AND T2.Value IS NOT NULL;
I'd suggest trying all of these options against the shape of your real data to find the most efficient version. Is also worth spending some time checking indexes etc to make these quicker.
One way to do it is using a common table expression and union all:
;WITH CTEInnerQuery AS
(
SELECT [someColumn] as [id], [someColumn2] as [name]
FROM [someInterface].[someSchema].[someTable]
WHERE [someID] = '12345'
), CTEInnerJson AS
(
SELECT *
FROM CTEInnerQuery
UNION ALL
SELECT NULL as [id], NULL as [name]
WHERE NOT EXISTS(SELECT 1 FROM CTEInnerQuery)
)
SELECT RA.id
,AllEngagementsAndJobCodes =
(
SELECT
(
SELECT
(...)
For JSON Path
) AS engagements
,
(
SELECT *
FROM CTEInnerJson
For JSON Path, INCLUDE_NULL_VALUES
) as jobCodes
For JSON Path, WITHOUT_ARRAY_WRAPPER
)
You can use this trick the resolve your problem
IF(NOT EXISTS (...))
BEGIN
SELECT 0 AS UserID
,N'' UserName
WHERE 1 = 2
RETURN;
END