0

I am needing to query for all the id values in the XML column below and needing some help doing that. How do I use the index values in my select statement? Any help/direction would be appreciated. Thanks.

Here is the XML file:

<rotation>
    <adjuster id="3381" index="0" />
    <adjuster id="7629" index="1" />
    <adjuster id="10087" index="2" />
    <adjuster id="10741" index="3" />
    <adjuster id="11544" index="4" />
    <adjuster id="12367" index="5" />
</rotation>

Here is my select statement but only getting the first value returned:

select 
    t.AssignmentRotation.value('(/rotation/adjuster/@id)[1]','varchar(max)') as adjuster_id 
from 
    dbo.CMS_AdjusterTeam t 
where 
    t.AdjusterTeamSysID IN (5, 6);

1 Answer 1

1

Following snippet illustrate how to extract all Id and index values:

declare @xml xml = N'<rotation>
    <adjuster id="3381" index="0" />
    <adjuster id="7629" index="1" />
    <adjuster id="10087" index="2" />
    <adjuster id="10741" index="3" />
    <adjuster id="11544" index="4" />
    <adjuster id="12367" index="5" />
</rotation>'

select Id = rt.aj.value('@id', 'varchar(5000)'),
    [Index] = rt.aj.value('@index', 'varchar(5000)')
    from (select XmlData = @xml) t
    cross apply t.XmlData.nodes('//rotation/adjuster') rt(aj)

So your final query would be looking like:

select rt.aj.value('@id','varchar(max)') as adjuster_id 
from dbo.CMS_AdjusterTeam t 
cross apply t.AssignmentRotation.nodes('//rotation/adjuster') rt(aj)
WHERE t.AdjusterTeamSysID IN (5, 6);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your time and help. I could not find this when googling.

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.