Tsql 从sql字符串中提取数据

Tsql 从sql字符串中提取数据,tsql,Tsql,我在一列中有以下数据。我想提取显示为542的“匹配细节”分数。问题是匹配分数也可能超过3个字符长。有人能帮忙吗 MatchingDetails score="542" maxScore="-96" matchRule="abcdef"><rule name="Person_Forename" score="279" /><rule name="Person_Surname" score="263" MatchingDetails score=“542”maxScore

我在一列中有以下数据。我想提取显示为542的“匹配细节”分数。问题是匹配分数也可能超过3个字符长。有人能帮忙吗

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

MatchingDetails score=“542”maxScore=“-96”matchRule=“abcdef”>一种方法是使用
charindex
patindex
子字符串的组合:

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)
结果:

542

如果您的数据是XML字符串,可能类似于

示例(已更正的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')
542