3

I have a column where I need to extract the remainder of a string starting from "_". Example below:

TF_Bskt Trade would need to be "Bskt Trade"
SSC_Equity would need to be "Equity"
etc ......

So I've got this far using the TF_Bskt Trade example:

SELECT SUBSTRING ('TF_Bskt Trade' ,CHARINDEX('_','TF_Bskt Trade')+1, length)

However i'm getting myself confused with how to make sure the length is from the start of after the _ and the end of the string so that it is dynamic? Basically so it caters for varying degrees of string length from after "_".

Any ideas of how to efficiently achieve this?

1 Answer 1

4

The following will do the job

 SELECT SUBSTRING ('TF_Bskt Trade', CHARINDEX('_', 'TF_Bskt Trade') + 1, LEN('TF_Bskt Trade'))

Because as the documentation for the length parameter of SUBSTRING states "If the sum of start and length is greater than the number of characters in expression, the whole value expression beginning at start is returned.".

Sign up to request clarification or add additional context in comments.

3 Comments

LEN doesn't count trailing spaces: LEN('A ') returns 1;
that's fine, i'm not interested in counting trailing spaces
why wouldn't it count trailing space oh, because microsoft

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.