Solved

delete records not working correctly in transact sql

Posted on 2012-03-13
5
303 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 setup several different housekeeping processes for a SQL Server.

895 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

17 Experts available now in Live!

Get 1:1 Help Now