1

Im trying to delete all rows that have a value in the column ' toevoeging '

I made this query:

DELETE FROM gebruiksoppervlakte WHERE toevoeging = NOT NULL;

Why is this not possible? So I want to keep the rows that have NULL Thanks!

3

3 Answers 3

7

You need to use IS NOT NULL instead of toevoeging = NOT NULL:

DELETE 
FROM gebruiksoppervlakte 
WHERE toevoeging IS NOT NULL;

NULL is something that lacks a value. So using IS NULL or IS NOT NULL means that your column is either lacking a value or not lacking a value.

Edit, this will delete everything in your table that has a value in the toevoeging column. If you have empty strings that you want to delete, then you will want to use:

DELETE 
FROM gebruiksoppervlakte 
WHERE toevoeging = '';

See Demo.

If you want to delete the rows with null values then you will use:

DELETE 
FROM gebruiksoppervlakte 
WHERE toevoeging IS NULL;

See Demo

Edit #2, based on our conversation in chat you are trying to delete all rows where there is a value but that value is not an empty string. Then the remaining rows need to be updated to null in the column. As a result you need to use both a delete and an update:

DELETE 
FROM gebruiksoppervlakte 
WHERE toevoeging IS NOT NULL
  and toevoeging <> '';


update gebruiksoppervlakte
set toevoeging = null;

See Demo

Sign up to request clarification or add additional context in comments.

5 Comments

@warnerst This will delete all rows where toevoeging is not null so if you have a value in each row, then it will delete everything. What are you trying to delete? Are the empty cells, null or empty strings?
Oh they where empty, but not ' NULL ' . So I think it should work now
@warnerst If you want to delete the rows with empty strings, see my edit.
@warnerst Are you trying to delete the null rows?
0

When using the WHERE clause to locate rows that have a NULL value, never use the “= NULL” comparison, instead always use the IS NULL or IS NOT NULL comparison operators.

So the correct query is :

DELETE FROM gebruiksoppervlakte WHERE toevoeging IS NOT NULL;

Comments

0

Because NULLs cannot be compared, the following works exactly the same as ... WHERE toevoeging IS NOT NULL :

DELETE FROM gebruiksoppervlakte
WHERE toevoeging = toevoeging
   ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.