Solved

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

Posted on 2010-11-17
3
246 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
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
Comment Utility
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 250 total points
Comment Utility
I personally don't use "LIKE" if I don't have to.

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

Author Comment

by:CMChalcraft
Comment Utility
Excellent. That has worked. Thank you both for your help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

11 Experts available now in Live!

Get 1:1 Help Now