7

I'm a DB newbie, and am struggling with this. I have an existing table with the following columns:

Showroom, Salesperson, Time1, Time2, Time3, Dte

I'm trying to delete all rows within the table that have either null or zero values in all 3 time 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)
            )

Thankfully I'm working on a test version of the database, as I wiped out all of the data. Any help would truly be appreciated.

1
  • See any of the answers below for the right approach. The reason what you wrote deleted all the data is that Exists is true whenever the statement within the exists returns any records.
    – Christoph
    Feb 10, 2011 at 21:36

7 Answers 7

23

The query should be formatted like this:

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 doing DELETE statements I think it is always best to first create your SELECT statement, and then change 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)
3
  • 4
    Abosolutely, never do a delete without running a select first to see what records will be affected, and putting it in a transaction without running the commit until you see how many records were affected is also helpful when running ad hoc delete queries.
    – HLGEM
    Feb 10, 2011 at 21:40
  • Thank you! That's a great tip and I will definitely do that from now on.
    – Kathy
    Feb 10, 2011 at 21:42
  • 2
    +1 for mentioning the "start with select first". A few too many bloopers on MyISAM tables (no rollback here) taught me to start with a select first, or at least start with the WHERE clause first, and then type in the UPDATE/DELETE table_name :)
    – Ronnis
    Feb 10, 2011 at 23:18
4

What you want is just;

DELETE myTable
WHERE
  (Time1 IS NULL OR Time1 = 0)
  AND (Time2 IS NULL OR Time2 = 0)
  AND (Time3 IS NULL OR Time3 = 0)
1
  • Thank you for your help, and for replying so soon.
    – Kathy
    Feb 10, 2011 at 21:44
1

The EXISTS is superfluous (as is the FROM - it isn't needed for DELETEs):

DELETE myTable 
WHERE ((Time1 IS NULL OR Time1 = 0) 
  AND (Time2 IS NULL OR Time2 = 0) 
  AND (Time3 IS NULL OR Time3 = 0))
1
  • Thank you for the prompt answer!
    – Kathy
    Feb 10, 2011 at 21:43
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 of your records are being deleted is because the EXISTS result is true as long as there exists a single record that has NULL or 0 for all three columns. Since your WHERE clause of the delete statement doesn't identify which records to delete, it is essentially the same as DELETE FROM myTable WHERE 1=1

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

Here is the query to delete blank rows as well as NULL

DELETE FROM table_name WHERE COL_NAME='' OR COL_NAME IS NULL;
0

What you can do is to create a select query in MS automatically, then find and replace symbols [ with (ISNULL([, ] with ],0) = 0). and to get AND condition for all numeric fields (ISNULL(Time1,0) = 0) AND.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.