Solved

How can I trim certain characters in SQL?

Posted on 2011-09-26
4
273 Views
Last Modified: 2012-06-27
How can I trim certain characters in a query?

I have a column that had some records containing the value: 12345-123
I only want to select the characters before the dash.

So I want to trim the dash and all characters at the right of the dash.
(So it returns the value: 12345)

Best regards,
Clyde.
0
Comment
Question by:Clyde_Radcliffe
[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
  • 2
4 Comments
 
LVL 6

Expert Comment

by:kswathi
ID: 36597606
0
 
LVL 6

Expert Comment

by:kswathi
ID: 36597616
0
 
LVL 3

Expert Comment

by:John_Arifin
ID: 36597972
Select Left(YourField, charindex('-', YourField) - 1) from YourTable
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 36599320
If the data will ALWAYS have a dash, then John_Arifin's suggestion will work splendidly.

If there is any possibility that there is no dash, would you want the whole string returned, or nothing?  If the former:

SELECT LEFT(SomeColumn, CHARINDEX('-', SomeColumn + '-') - 1) AS Answer
FROM SomeTable

Open in new window


If the latter, this excludes records where there is no dash:

SELECT LEFT(SomeColumn, CHARINDEX('-', SomeColumn) - 1) AS Answer
FROM SomeTable
WHERE SomeColumn LIKE '%-%'

Open in new window


and this returns a null:

SELECT CASE 
    WHEN SomeColumn LIKE '%-%' THEN LEFT(SomeColumn, CHARINDEX('-', SomeColumn) - 1)
    ELSE NULL END AS Answer
FROM SomeTable

Open in new window


and this returns a zero length string:

SELECT CASE 
    WHEN SomeColumn LIKE '%-%' THEN LEFT(SomeColumn, CHARINDEX('-', SomeColumn) - 1)
    ELSE '' END AS Answer
FROM SomeTable

Open in new window

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

623 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