Solved

How can I trim certain characters in SQL?

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backup and restore 21 30
sql2016-WIn10: standard,for SQL servc-account.. 51 31
How can I get the entire database script? 7 14
MS SQL + group by time 4 15
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

821 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