remove all NULL valued rows from table? - sql

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

Related

Delete many columns with null

I have a table with 34 columns.
First 4 columns form the primary key (thus they do not have nulls in them) and rest 30 columns have data in them also nulls.
I wish to delete all the rows where the other 30 columns are null.
A way would be -
delete from my_table
where column_30 is null
and columns_29 is null
and column_28 is null
and column_27 is null
...
...
...
Is there any easy way to do this without mentioning all 30 column names?
No, that's impossible. You could shorten the code a bit but pay a penalty in sargability - using coalesce:
delete from my_table
where coalesce(column_30, column_29, column_28.....) is null
As Damien wrote in his comment - this will only work if all columns are of compatible types, meaning SQL Server can implicitly convert between them.
coalesce receives a list of expressions and returns the first not-null one, or null if they all are, so you can use it as some sort of a shorthand:
DELETE FROM mytable
WHERE COALESCE(column_30, column_29, column_28, ...) IS NULL
Another approach of doing it. (Assuming that the datatype of all the columns is same)
delete t
from my_table t
where (select top 1 1 as col
from (values (T.column_30),
(T.columns_29),
(T.columns_28 ),
(T.columns_27 )) v(x)
where x is null) = 1

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.

How do you do a SQL update a table using a group by function, and length filter?

I'm trying to update specific records within two columns by filtering on the length of the column as well as the max of a third column to grab the latest record. I'm aware that I can't use a group by function like this, and have been trying to figure out how to do it using a sub query.
UPDATE TABLE
SET COLUMN1 = 'NULL', COLUMN2 = 'NULL'
WHERE LENGTH (COLUMN1) BETWEEN 10 AND 20
AND MAX (COLUMN3)
SELECT *
FROM TABLE
WHERE LENGTH(COL1) BETWEEN 10 AND 20
AND MAX(COLUMN3)
This returns what I want, for example purposes. Their are two columns within the table that I want to set specific rows with a specific length to null, also filtered with the max function to grab the latest record.
I suspect you want this:
UPDATE TABLE
SET COLUMN1 = 'NULL', COLUMN2 = 'NULL'
WHERE LENGTH(COLUMN1) BETWEEN 10 AND 20 AND
COLUMN3 = (SELECT MAX(t2.COLUMN3)
FROM TABLE t2
WHERE LENGTH(t2.COLUMN1) BETWEEN 10 AND 20
);
Also, why are you setting the columns to the string 'NULL'? If you want a NULL value then drop the single quotes.

SQL Server inconsistent results over 2 columns using = and <>

I am trying to replace a manual process with an SQL-SERVER (2012) based automated one. Prior to doing this, I need to analyse the data in question over time to produce some data quality measures/statistics.
Part of this entails comparing the values in two columns. I need to count where they match and where they do not so I can prove my varied stats tally. This should be simple but seems not to be.
Basically, I have a table containing two columns both of which are defined identically as type INT with null values permitted.
SELECT * FROM TABLE
WHERE COLUMN1 is NULL
returns zero rows
SELECT * FROM TABLE
WHERE COLUMN2 is NULL
also returns zero rows.
SELECT COUNT(*) FROM TABLE
returns 3780
and
SELECT * FROM TABLE
returns 3780 rows.
So I have established that there are 3780 rows in my table and that there are no NULL values in the columns I am interested in.
SELECT * FROM TABLE
WHERE COLUMN1=COLUMN2
returns zero rows as expected.
Conversely therefore in a table of 3780 rows, with no NULL values in the columns being compared, I expect the following SQL
SELECT * FROM TABLE
WHERE COLUMN1<>COLUMN2
or in desperation
SELECT * FROM TABLE
WHERE NOT (COLUMN1=COLUMN2)
to return 3780 rows but it doesn't. It returns 3709!
I have tried SELECT * instead of SELECT COUNT(*) in case NULL values in some other columns were impacting but this made no difference, I still got 3709 rows.
Also, there are some negative values in 73 rows for COLUMN1 - is this what causes the issue (but 73+3709=3782 not 3780 my number of rows)?
What is a better way of proving the values in these numeric columns never match?
Update 09/09/2016: At Lamaks suggestion below I isolated the 71 missing rows and found that in each one, COLUMN1 = NULL and COLUMN2 = -99. So the issue is NULL values but why doesn't
SELECT * FROM TABLE WHERE COLUMN1 is NULL
pick them up? Here is the information in Information Schema Views and System Views:
ORDINAL_POSITION COLUMN_NAME DATA_TYPE CHARACTER_MAXIMUM_LENGTH IS_NULLABLE
1 ID int NULL NO
.. .. .. .. ..
7 COLUMN1 int NULL YES
8 COLUMN2 int NULL YES
CONSTRAINT_NAME
PK__TABLE___...
name type_desc is_unique is_primary_key
PK__TABLE___... CLUSTERED 1 1
Suspect the CHARACTER_MAXIMUM_LENGTH of NULL must be the issue?
You can find the count based on the below query using left join.
--To find COLUMN1=COLUMN2 Count
--------------------------------
SELECT COUNT(T1.ID)
FROM TABLE T1
LEFT JOIN TABLE T2 ON T1.COLUMN1=T2.COLUMN2
WHERE t2.id is not null
--To find COLUMN1<>COLUMN2 Count
--------------------------------
SELECT COUNT(T1.ID)
FROM TABLE T1
LEFT JOIN TABLE T2 ON T1.COLUMN1=T2.COLUMN2
WHERE t2.id is null
Through the exhaustive comment chain above with all help gratefully received, I suspect this to be a problem with the table creation script data types for the columns in question. I have no explanation from an SQL code point of view, as to why the "is NULL" intermittently picked up NULL values.
I was able to identify the 71 rows that were not being picked up as expected by using an "except".
i.e. I flipped the SQL that was missing 71 rows, namely:
SELECT * FROM TABLE WHERE COLUMN1 <> COLUMN 2
through an except:
SELECT * FROM TABLE
EXCEPT
SELECT * FROM TABLE WHERE COLUMN1 <> COLUMN 2
Through that I could see that COLUMN1 was always NULL in the missing 71 rows - even though the "is NULL" was not picking them up for me when I ran
SELECT * FROM TABLE WHERE COLUMN1 IS NULL
which returned zero rows.
Regarding the comparison of values stored in the columns, as my data volumes are low (3780 recs), I am just forcing the issue by using ISNULL and setting to 9999 (a numeric value I know my data will never contain) to make it work.
SELECT * FROM TABLE
WHERE ISNULL(COLUMN1, 9999) <> COLUMN2
I then get the 3780 rows as expected. It's not ideal but it'll have to do and is more or less appropriate as there are null values in there so they have to be handled.
Also, using Bertrands tip above I could view the table creation script and the columns were definitely set up as INT.

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

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