I have an xml field that I need to select the inner xml from a node as a string regardless of what the inner xml is (in this case, it's an html image tag). I'm able to get plain text with no problems but when I have something with the image tag in it, I think it's treating it as a child node so it's not returning it.
declare @myXml xml = '<question><prompt><img src="someImage.png" /></prompt></question>';
select q.value('(prompt/text())[1]', 'nvarchar(max)') as prompt from @myXml.nodes('//question') qs(q);
The above is returning null. What I'm wanting is the actual contents of the node as a string.
This should work....
text()
only returns text values from the child nodes, whilenode()
returns the whole enchilada.Add in a
CAST
to varchar, and you're done!