1

I have the following data in a column. I want to extract the 'matching details' score just showing as 542. The problem is the matching score can also be more than 3 characters long. Can someone help?

 MatchingDetails score="542" maxScore="-96" matchRule="abcdef"><rule name="Person_Forename" score="279" /><rule name="Person_Surname" score="263"

2 Answers 2

1

One way is to use a combination of charindex, patindex, and substring:

DECLARE @S varchar(100) = 'MatchingDetails score="542" maxScore="-96" matchRule="abcdef">'


SELECT SUBSTRING(@S, 
                 patindex('% score="%', @S) + 8,
                 charindex('"', @S, patindex('% score="%', @S) + 9) - patindex('% score="%', @S) - 8)

Result:

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

1 Comment

Thank you so much, that's so helpful!
1

If your data is an XML string, perhaps something like this

Example (corrected xml)

Declare @S varchar(max) = '
<MatchingDetails score="542" maxScore="-96" matchRule="abcdef" >
    <rule name="Person_Forename" score="279"></rule>
    <rule name="Person_Surname" score="263"></rule>
</MatchingDetails>
'

Select convert(xml,@S).value('MatchingDetails[1]/@score','int')

Returns

542

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.