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