views:

132

answers:

3

How do I return the everything in a string from a sql query before a certain character?

My data looks like this:

HD TV HM45VM - HDTV widescreen television set with 45" lcd

I want to limit or truncate the string to include everything before the dash.

So the final result would be "HD TV HM45VM"

+6  A: 

Use SUBSTRING, but you'll have to use either CHARINDEX or PATINDEX to get the location of the character you want the substring to stop at:

SELECT SUBSTRING('HD TV HM45VM - HDTV widescreen television set with 45" lcd', 
                  0, 
                  CHARINDEX('-', 'HD TV HM45VM - HDTV widescreen television set with 45" lcd'))

To do the opposite - strip everything to the left of the hyphen - use:

SELECT SUBSTRING('HD TV HM45VM - HDTV widescreen television set with 45" lcd', 
                  CHARINDEX('-', 'HD TV HM45VM - HDTV widescreen television set with 45" lcd') + 1,
                  LEN('HD TV HM45VM - HDTV widescreen television set with 45" lcd'))

Effectively, make the location of the hyphen to be the starting point. Then you can use either LEN or DATALENGTH functions.

OMG Ponies
You might need to trim spaces afterwards - look at using LTRIM or RTRIM if that's the case.
OMG Ponies
Does the trick. Thanks a bunch. How would I do do the opposite? How could I strip everything to left of the dash?
jeff
I recommend LEN(). DATALENGTH() will fail if the OP either now or later plans to use `nvarchar`. Also, if the field is `varchar(1000)`, you can simply provide 1000 as the third argument to SUBSTRING() and avoid the LEN() call.
richardtallent
Getting incorrect syntax on the last code block posted...
jeff
@jeff: Forgot the comma - try now.
OMG Ponies
+2  A: 

Dont forget that LEFT exists

select LEFT('HD TV HM45VM - HDTV widescreen television set with 45" lcd',CHARINDEX('-', 'HD TV HM45VM - HDTV widescreen television set with 45" lcd ')-1)
astander
Upvoted for using LEFT() rather than SUBSTRING() (a better fit IMHO for readability), but you should substract 2 rather than 1. CHARINDEX() is 1-based and you only searched for the dash, not the preceding space.
richardtallent
True, but will the space be there, then rahter use a **RTRIM**
astander
A: 

Ok, I'll take a stab at it too and see if I can simplify:

SELECT LEFT(myfield', CHARINDEX(' - ', myfield) - 1)

By wrapping the - in spaces, the delimiter is less likely to be tripped up, and there's no need to trim the result.

Caveat: this, like the other responses, will result in an error if myfield does not include the - delimiter.

Ultimately, if there is always a dashed delimiter in your table, you should consider having two fields.

richardtallent
Spaces might not be present.
astander
astander brings up a good point - PATINDEX would be a better choice for that, because it allows psuedo-regexes
OMG Ponies
@astander - where do you get that from the OP? Also, while PATINDEX() allows use of wildcards, it isn't capable of expressions like "an optional *space*, followed by a dash". CHARINDEX() will perform better given the input data provided. Searching for the space will avoid an edge-case bug that is entirely likely if the first portion of the string contains a dash (since many product codes in the wild may contain dashes, but not usually prefixed by a space). If the data are all delimited consistently, string parsing should exploit the more robust delimiter to avoid false positive splits.
richardtallent