1

I have a Variable that contains dynamic string. I want to get the word that is equal to COATitle only.

DECLARE @String Varchar(250)
 SET @String = 'COATitle = ''Hello'' AND Date = ''2018-10-09'' AND Name = ''Warner Bros'''

 SELECT @String AS String

This string is being provided from front end so user can enter in different order as well. For example

SET @String = 'Date = ''2018-10-09'' AND COATitle = ''Hello Tonia'' AND Name = ''Warner Bros'''
SET @String = 'COATitle = ''Hello'' AND Name = ''Warner Bros'' Date = ''2018-10-09'' AND '
SET @String = 'COATitle = ''Hello World'' AND Date = ''2018-10-09'' AND Name = ''Warner Bros'''
SET @String = 'COATitle = ''Piece of Cake'' AND Date = ''2018-10-09'' AND Name = ''Tim Martin'''

Expected output for above mentioned Strings are:

1: Hello Tonia
2: Hello
3: Hello World
4: Piece of Cake

1 Answer 1

1

One approach here is to just use base string functions in SQL Server. We can take a substring of the input, using the starting point as the text which immediately follows COATitle = ', continuing until just before the very next closing single quote.

SELECT
    word,
    SUBSTRING(word,
              CHARINDEX('COATitle = ''', word) + 12,
              CHARINDEX('''', word, CHARINDEX('COATitle = ''', word) + 12) -
                  CHARINDEX('COATitle = ''', word) - 12) AS target
FROM yourTable;

enter image description here

Demo

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

Comments

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.