Using TSQL functions to split a delimited string

Answered a recent question on the forums that I thought was worthy of a blog entry.

Question

Looking for a function or a method to basically take the following strings and provide me with the string after the minus "-" sign.  TRIM or MID doesn't work in the following cases because the number of characters before and after the minus sign are not constant.

ie. ABC123-ABCDEFGH
OR AB12-ABCDEF

Been awhile since I had to dig in to this, so appreciate if someone who does this stuff on a regular basis could share.

Reply

You basically need to combine functions...

declare @Str as varchar(20)

set @Str = 'ABC123-ABCDEFGH'

-- Return the right part of the string, where the total length minus the position to the '-' character
Select RIGHT(@Str, LEN(@Str) - CHARINDEX('-', @Str))

result: ABCDEFGH

Published Wednesday, March 10, 2010 9:51 AM by sthompson
Filed under: ,

Comments

# re: Using TSQL functions to split a delimited string

Friday, April 30, 2010 10:55 AM by naquino353

Nice for a string with 2 sides, but what about this?

abc-123-ghi

# re: Using TSQL functions to split a delimited string

Tuesday, May 04, 2010 11:29 AM by sthompson

Which part do you want? ;-)

Powered by Community Server (Commercial Edition), by Telligent Systems