Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

delete records not working correctly in transact sql

Posted on 2012-03-13
5
Medium Priority
?
310 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 2000 total points
ID: 37714728
delete from tblmaster where LEFT(Filename,2) = '9-' and LEFT(Filename,6) != '9-5400'
0
 
LVL 40

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

916 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