sql select specific text from column

I am trying to select just the "Value" in the column.

Column name is ResultParmsXML. Below is an example of data returned.
<Params><Param Name="The stuck resource" Value="SERVER01" /><Param Name="The stuck resource" Value="SERVER04" /><Param Name="The stuck resource" Value="SERVER06" /><Param Name="The stuck resource" Value="SERVER03" /><Param Name="The stuck resource" Value="SERVER02" /></Params>

here is what i have so far but only returns the first instance of the Value. As you can see above there could be multiple instances.
SELECT SUBSTRING(ResultParamsXML, PATINDEX('%Value=%',ResultParamsXML) +7, 12)
  FROM Test_ResultsLog
  where TestId=1000231
  and ResultCode=1
  order by RunTime desc

I am looking for results like this:
ResultParmsXML
WMVPDAXAPP02, WMVPDAXAPP04,WMVPDAXAPP05
WMVPDAXAPP01,WMVPDAXAPP02,WMVPDAXAPP03
jacobJLAsked:
Who is Participating?
 
Chris LuttrellSenior Database ArchitectCommented:
Ah, Anthony beat me to the main query that works if you can make your column XML in the first place.
If not and you have to start with an VARCHAR(MAX) or equivalent column you can structure a query like this ( I just wrapped the base table in a nested query casting the Varchar into an XML)
SELECT c.query('.') AS XMLFragment, c.value('(@Value)', 'varchar(40)') AS yourValue
  FROM (SELECT TestId, ResultCode, RunTime, CAST(ResultParamsXML AS XML) ResultParamsXML FROM Test_ResultsLog) x
  CROSS APPLY ResultParamsXML.nodes('/Params/Param') AS t(c)
  where TestId=1000231
  and ResultCode=1
  order by RunTime DESC

Open in new window

0
 
Anthony PerkinsCommented:
Try it like this:
SELECT  T.C.value('@Value', 'varchar(50)') Value
FROM    Test_ResultsLog
        CROSS APPLY ResultsParamsXml.nodes('Params/Param') T (C)
WHERE   TestId = 1000231
        AND ResultCode = 1
ORDER BY RunTime DESC

Open in new window

0
 
Anthony PerkinsCommented:
This is how I tested it:
DECLARE @Test_ResultsLog table (TestId integer, ResultCode bit, RunTime integer, ResultsParamsXml xml)
INSERT @Test_ResultsLog(TestId, ResultCode, RunTime, ResultsParamsXml)
VALUES (1000231, 1, 2, '<Params><Param Name="The stuck resource" Value="SERVER01" /><Param Name="The stuck resource" Value="SERVER04" /><Param Name="The stuck resource" Value="SERVER06" /><Param Name="The stuck resource" Value="SERVER03" /><Param Name="The stuck resource" Value="SERVER02" /></Params>')

SELECT  T.C.value('@Value', 'varchar(50)') Value
FROM    @Test_ResultsLog
        CROSS APPLY ResultsParamsXml.nodes('Params/Param') T (C)
WHERE   TestId = 1000231
        AND ResultCode = 1
ORDER BY RunTime DESC

Open in new window

And this is the output:
SERVER01
SERVER04
SERVER06
SERVER03
SERVER02
0
 
jacobJLAuthor Commented:
The column type was not xml so i had to use this one. I have no idea what is going on in this query but it got the results i was looking for.

Thank you both for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.