SQL server - delete rows in which several columns have zero or zero value

I am new to DB and I am struggling with this. I have an existing table with the following columns:

Showroom, Salesperson, Time1, Time2, Time3, Dte

I am trying to delete all rows in a table that have zero or zero values ​​in all three temporary columns. I tried the following:

DELETE FROM myTable 
WHERE EXISTS(
              SELECT * 
              FROM myTable 
              WHERE (Time1 IS NULL OR Time1 = 0) 
                AND (Time2 IS NULL OR Time2 = 0) 
                AND (Time3 IS NULL OR Time3 = 0)
            )

Fortunately, I am working on a test version of the database, since I destroyed all the data. Any help would be really appreciated.

+3
source share
5 answers

The request should be formatted as follows:

DELETE 
FROM myTable 
WHERE (Time1 IS NULL OR Time1 = 0) 
AND (Time2 IS NULL OR Time2 = 0) 
AND (Time3 IS NULL OR Time3 = 0)

When making DELETEstatements I think It is always best to create your statement SELECTand then modify it.

SELECT * --If this returns the correct records, simply change to DELETE
FROM myTable 
WHERE (Time1 IS NULL OR Time1 = 0) 
AND (Time2 IS NULL OR Time2 = 0) 
AND (Time3 IS NULL OR Time3 = 0)
+15
source

, , :

DELETE myTable
WHERE
  (Time1 IS NULL OR Time1 = 0)
  AND (Time2 IS NULL OR Time2 = 0)
  AND (Time3 IS NULL OR Time3 = 0)
+3

EXISTS ( FROM - DELETE s):

DELETE myTable 
WHERE ((Time1 IS NULL OR Time1 = 0) 
  AND (Time2 IS NULL OR Time2 = 0) 
  AND (Time3 IS NULL OR Time3 = 0))
+1

Try this instead:

DELETE 
FROM myTable 
WHERE 
  (Time1 IS NULL OR Time1 = 0) AND 
  (Time2 IS NULL OR Time2 = 0) AND 
  (Time3 IS NULL OR Time3 = 0)

The reason all your records are deleted is because the EXISTS result is true if there is one record with NULL or 0 for all three columns. Since the WHERE clause in the delete statement does not determine which records to delete, this is essentially the same asDELETE FROM myTable WHERE 1=1

+1
source
DELETE myTable WHERE 
(ISNULL(Time1,0) = 0) AND (ISNULL(Time2,0) = 0) AND (ISNULL(Time3,0) = 0)
0
source

Source: https://habr.com/ru/post/1791326/


All Articles