0

I have a text field which has entries of variable length of the form:

"house:app.apx&resultid=1234,clientip"

or

"tost:app.apx&resultid=123,clientip"

or

"airplane:app.apx&resultid=123489,clientip"

I'm trying to pick out the numbers between resultid='...',clientip no matter what the rest of the string looks like. So in this example it would be the numbers:

1234
123
12389

the part of the string of the form resultid='...',clientip always stays the same except the length of the number can vary.

3
  • Use substring together with charindex. Commented Jul 14, 2015 at 19:02
  • Is this data you are retrieving from the database or are you trying to do this in a stored procedure within the database? Commented Jul 14, 2015 at 19:04
  • What have you tried? Commented Jul 14, 2015 at 19:54

3 Answers 3

1

This should work:

-- test data
DECLARE @t table (String VARCHAR(100))
INSERT @t VALUES 
('"house:app.apx&resultid=1234,clientip"'),
('"tost:app.apx&resultid=123,clientip"'),
('"airplane:app.apx&resultid=123489,clientip"')


SELECT 
  SUBSTRING(
    String, 
    CHARINDEX('resultid=', String) + 9, 
    CHARINDEX(',clientip', String) - CHARINDEX('resultid=', String) - 9
  ) 
FROM @t

-- Result
1234
123
123489

You might want to add some kind of check so that you don't process null values.

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

Comments

0

It can be done like below using string functions

select SUBSTRING("house:app.apx&resultid=1234,clientip",
                           CHARINDEX('=',"house:app.apx&resultid=1234,clientip"),
                           CHARINDEX(',',"house:app.apx&resultid=1234,clientip") - CHARINDEX('=',"house:app.apx&resultid=1234,clientip"));

Comments

0

Similar to @Rahul but somewhat different and using PATINDEX:

declare @start varchar(20) = '%resultid=%'
declare @end varchar(20) = '%,clientid%'
SELECT REPLACE(REPLACE(SUBSTRING(Field,PATINDEX(@start,Field)),@start,''),@end,'')

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.