0

I am working with xml datatype columns in SQL Server. I know how to extract the values from XML columns by using query() or value() functions but my problem is slightly different.

In my database table the XML has no fixed pattern. This means, the XML tags are not fixed and varying in most of the records.

As an example:

<Types>
  <request>
    <Rollcount>34</Rollcount>
  </request>
</Types>

<Types>
  <general>
    <Rollcount>5</Rollcount>
  </general>
</Types>

<Types>
  <land>
     <Rollcount>21</Rollcount>
  <land>
</Types>

In above example you can see, XML has different tags like <request>, <general> and <land>. There is no fixed pattern.

Now I want to extract a value of <Rollcount>. Could you please tell me how can I do this?

1 Answer 1

1

You could wildcards. If we assume that the top nodes is always Types, and Rollcount is always the 3rd level then you could do this:

DECLARE @XML xml ='<Types>
  <general>
    <Rollcount>5</Rollcount>
  </general>
</Types>';

SELECT X.RC.value('.','int')
FROM @XML.nodes('Types/*/Rollcount')X(RC);

db<>fiddle

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

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.