Solved

delete records not working correctly in transact sql

Posted on 2012-03-13
5
304 Views
Last Modified: 2012-03-13
I have a table with a column named filename. I am trying to delete all of my records that start with a "9-" except not delete records that start with "9-5400". This statement doesn't work:

delete from tblmaster where substring(filename,1,2) = '9-' and substring(filename,1,6) != '9-5400'

Can someone help me rephrase this string so it ignores the 9-5400 but deletes everything else that starts with 9-.

Thanks!
0
Comment
Question by:samic400
5 Comments
 
LVL 15

Accepted Solution

by:
Anuj earned 500 total points
ID: 37714728
delete from tblmaster where LEFT(Filename,2) = '9-' and LEFT(Filename,6) != '9-5400'
0
 
LVL 39

Expert Comment

by:lcohan
ID: 37714786
Try this then replace select with delete

select * from tblmaster
where substring(ltrim(filename),1,2) = '9-' and substring(ltrim(filename),1,6) != '9-5400'
0
 
LVL 25

Expert Comment

by:jogos
ID: 37715012
A little modification on previous comment (credits!),
Always include the 'modifications' you do in the select list.  And if you don't get what you want, one by one comment out the filter until you see where the problem is.
As Icohan already suggested there could be a space before the filename

select substring(filename,1,2),substring(filename,1,6), *
from tblmaster
where substring(ltrim(filename),1,2) = '9-'
and substring(ltrim(filename),1,6) != '9-5400'


And before trying a delete .... always try it as a select first so that you can verify it is realy what you want.
0
 
LVL 25

Expert Comment

by:jogos
ID: 37715024
But I would write it as a LIKE, but it won't change the outcome.

where filename like '9-%'
and filename not like '9-5400%'
0
 
LVL 13

Author Closing Comment

by:samic400
ID: 37715043
Thank you!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question