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?

有帮助吗?

解决方案

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.".

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top