Skip to content
Advertisement

Retrieve Specific Text from String where Search String Exists and Not Exists

I’ve got the following code. It returns the correct result for the first two test strings, where it should return the text between the two ****.

The 3rd string returns ” abcd two two ****” rather than ” abcd two two ” The 4th string returns “Invalid length parameter passed to the LEFT or SUBSTRING function.”

Any ideas?

Advertisement

Answer

The search terms are four asterisks. This approach uses CHARINDEX, once forwards to find the first set, and then with REVERSE to find the last occurrence.

Output

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement