?
Solved

How do select the first part of a string in Microsoft SQL

Posted on 2010-11-17
3
Medium Priority
?
251 Views
Last Modified: 2012-05-10
I have 10,000 SiteID records. All those beginning with "18" are closed records which I want to exclude. I have tried this but is doesn't work. Any ideas?

SELECT     SiteID, site_name, CustomerID
FROM         dbo.tblSite
WHERE     (LEFT(dbo.SiteID, 2) <> '18')
0
Comment
Question by:CMChalcraft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 34156510
SELECT     SiteID, site_name, CustomerID
FROM         dbo.tblSite
where  siteid not like '18%'

or

SELECT     SiteID, site_name, CustomerID
FROM         dbo.tblSite
where  ltrim(siteid) not like '18%'
0
 
LVL 1

Assisted Solution

by:jb428j
jb428j earned 1000 total points
ID: 34157759
I personally don't use "LIKE" if I don't have to.

WHERE SUBSTRING(SiteID,1,2) <> 18
0
 

Author Comment

by:CMChalcraft
ID: 34163675
Excellent. That has worked. Thank you both for your help.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 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