psysky
psysky

Reputation: 3195

Delete rows using case when condition in SQL Server

Say I write this query

select
    x1, x2, x3 act
from 
    mytab

I need delete rows using any condition

case when x1=b, and act=0, then delete these rows

At the same time, i need to delete rows that contain a null value by x1 column.

How can I do that?

Upvotes: 2

Views: 20506

Answers (3)

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31991

For deleting rows,use Delete statement instead of select and the table name and then where clause for putting condition

 Detele t1
     from mytab t1
    where 
     (  x1=b  and act=0
     ) OR x1 IS NULL

here t1 is alias of table mytab

Upvotes: 0

Hello World
Hello World

Reputation: 2907

In MySQL use where clause

DELETE FROM mytab 
WHERE x1 = b AND act=0 OR x1 IS NULL;;

Just execute this SQL and done. It will delete rows from database matching these conditions.

Upvotes: 0

Squirrel
Squirrel

Reputation: 24783

You specified the condition in WHERE clause

for the condition that you required

DELETE d
FROM   mytab d
WHERE (
          d.x1 = 'b' AND d.act = 0
      )
OR    d.x1 is null

Upvotes: 4

Related Questions