Solved

How can I trim certain characters in SQL?

Posted on 2011-09-26
4
256 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
  • 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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

762 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

17 Experts available now in Live!

Get 1:1 Help Now