How to extract a text between n character, may have texts between n character multiple times using Sql server

  Kiến thức lập trình

I need to write a stored procedure which takes response from API and save values in local DB.For example, I have a string like this “Image is Non-compliant nFace Covered nIncorrect Lighting n”. I need to extract the text between n character like Face Covered and Incorrect Lighting. How to write a sql query to extract the text and the string length may varies.

I tried with
SELECT SUBSTRING(‘Image is Non-compliant nFace Covered nIncorrect Lighting n’,
CHARINDEX(‘n’,’Image is Non-compliant nFace Covered nIncorrect Lighting n’)+2,
CHARINDEX(‘n’,’Image is Non-compliant nFace Covered nIncorrect Lighting n’,CHARINDEX(‘n’,’Image is Non-compliant nFace Covered nIncorrect Lighting n’)+1) –
CHARINDEX(‘n’,’Image is Non-compliant nFace Covered nIncorrect Lighting n’)-2) as My_String.

But the output I’m getting only Face Covered.
But I need to extract whatever the text which is between n dynaically

New contributor

Sindhu C L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT