patternsqlMajor
Delete from table rows where any of the column field is null
Viewed 0 times
rowsfieldthedeletecolumnnullwhereanyfromtable
Problem
Is there a way to delete a row from a table where any of the column field is null without specifying explicitly which column is null?
I am using postgreSQL.
Here's my relation schema:
Thanks
I am using postgreSQL.
Here's my relation schema:
Column | Type | Modifiers
--------------+---------+----------------------------------------------------------------------
id | integer | not null default nextval('aurostat.visitor_center_id_seq'::regclass)
date | date |
persons | integer |
two_wheelers | integer |
cars | integer |
vans | integer |
buses | integer |
autos | integer |Thanks
Solution
I see two ways of doing that:
With plain standard SQL, simply list all columns and combine that with an OR:
Another (Postgres specific) solution is the comparison of the whole row with
will return only rows where all columns are not null. You want the opposite, so you need to negate that
With plain standard SQL, simply list all columns and combine that with an OR:
delete from the_table
where date is null
or persons is null
or two_wheelers is null
or cars is null
or vans is null
or buses is null
or autos is null;Another (Postgres specific) solution is the comparison of the whole row with
NOT NULLselect *
from the_table
where the_table is not null;will return only rows where all columns are not null. You want the opposite, so you need to negate that
where not (the_table is not null) The condition where the_table is null is something different - that only matches rows where all columns are null. delete from the_table
where not (the_table is not null);Code Snippets
delete from the_table
where date is null
or persons is null
or two_wheelers is null
or cars is null
or vans is null
or buses is null
or autos is null;select *
from the_table
where the_table is not null;delete from the_table
where not (the_table is not null);Context
StackExchange Database Administrators Q#143959, answer score: 23
Revisions (0)
No revisions yet.