Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an entire row if one column is empty

Tags:

sql

Say I have the following table:

Tree     Park      Slide       
1           1        1
1           1        1
                     1
1                    1

What kind of code would I use to remove the rows that have empty columns such that I would just have the following result

 Tree     Park      Slide       
    1           1        1
    1           1        1

I am new to sql and was wondering on some tips on how to write this code. Would I use a case statement such that

Case
WHEN Tree IS NULL OR Park IS NULL
--Then what would I say to remove the row
like image 229
user3107674 Avatar asked Dec 20 '25 19:12

user3107674


1 Answers

Use DELETE to delete a row and check it with IS NULL in the WHERE clause.

DELETE FROM dbo.TableName
WHERE Tree IS NULL OR Park IS NULL OR Slide IS NULL

If you also want to delete rows where one of these columns is not null but empty text(if it's a varchar column):

DELETE FROM dbo.TableName
WHERE (Tree IS NULL OR Tree = '') 
 OR   (Park IS NULL OR Park = '') 
 OR   (Slide IS NULL OR Slide = '') 
like image 129
Tim Schmelter Avatar answered Dec 22 '25 12:12

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!