Solved

delete records not working correctly in transact sql

Posted on 2012-03-13
5
300 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
Comment Utility
delete from tblmaster where LEFT(Filename,2) = '9-' and LEFT(Filename,6) != '9-5400'
0
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now