0

I have a block of XML in a column of type xml in SQL Server database that looks like this:

<p>
  <k>field</k>
  <v>user</v>
</p>
<p>
  <k>action</k>
  <v>delete+set</v>
</p>
<p>
  <k>prompt</k>
  <v>smith</v>
</p>

I have no control over how the XML is put into the database, I can only query it.

I would like to write a select statement that returns "smith" - the value in the v tag that is nested within a element that contains a k element with a value of "prompt".

The name of this column is "apple" and the table is "rule".

Any help would be appreciated.

Thanks!

6
  • stackoverflow.com/questions/899313/… Check this answer. Commented Feb 16, 2016 at 23:34
  • You're looking for something like SELECT A.B.value('v[1]','NVARCHAR(255)') FROM tblName CROSS APPLY col.nodes('/p') A(B) WHERE A.B.value('k[1]','NVARCHAR(255)') = 'prompt' but it depends on what other stuff is in the XML. Commented Feb 16, 2016 at 23:41
  • FZE - that returns all the values, I just want one. Commented Feb 16, 2016 at 23:43
  • ZLK - that is the entire XML. I'm not sure what the A.B. means. I've updated my post to mention the name of the field is called "apple". Commented Feb 16, 2016 at 23:45
  • ZLK got it. SELECT A.B.value('v[1]','NVARCHAR(255)') FROM [rule] CROSS APPLY xml.nodes('/p') A(B) WHERE A.B.value('k[1]','NVARCHAR(255)') = 'prompt' Commented Feb 16, 2016 at 23:49

1 Answer 1

1

Another way to skin the cat.

DECLARE @x XML = '<p>
  <k>field</k>
  <v>user</v>
</p>
<p>
  <k>action</k>
  <v>delete+set</v>
</p>
<p>
  <k>prompt</k>
  <v>smith</v>
</p>'

SELECT @X.value('((//k)[text()="prompt"]/../v)[1]', 'varchar(100)')
Sign up to request clarification or add additional context in comments.

1 Comment

And this is a shorter way to skin the cat: .value('(/p[k="prompt"]/v)[1]', 'varchar(100)'). Should be faster to, because there is no back and forth navigation.

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.