Solved

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

Posted on 2010-11-17
3
249 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
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 250 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHOWPLAN permission denied in database 'AdventureWorks'. 13 117
Caste datetime 2 69
Strange msg in the SSMS pane 13 60
Trying to understand why my Index is so large 12 21
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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