1

I have XML like this in a column of a SQL Server table:

<Sales>
  <customer>
    <custID>6886903</custID>
    <placeID>143144</placeID>UNKNOWN</customer>

</Sales>

How to retrieve UNKNOWN from above when it doesn't have any element name?

1
  • At this link you find some examples about text() nodes and how the might float within their element. Commented Aug 15, 2018 at 6:36

1 Answer 1

4

Actually, the UNKNOWN text belongs to the customer element.

DECLARE @data XML
SELECT  @data = '<Sales>
  <customer>
    <custID>6886903</custID>
    <placeID>143144</placeID>UNKNOWN</customer>

</Sales>'

SELECT  p.value('(./custID)[1]' , 'int') AS custID,
        p.value('(./placeID)[1]', 'int') AS placeID,
        p.value('(./text())[1]' , 'varchar(max)') AS customerName
FROM    @data.nodes('/Sales/customer') t(p)
Sign up to request clarification or add additional context in comments.

1 Comment

Good answer, you might read this link about some details on text()

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.