3

I have an XQuery as under

DECLARE @x XML
SELECT @x = '<PartnerEmails>
<Email>[email protected]</Email>
<Email>[email protected]</Email>
</PartnerEmails>'
SELECT @x.query('data(PartnerEmails/Email)').value('.','varchar(100)') AS Val

Actual Output:

Val
[email protected] [email protected]

Expected Output

[email protected]
[email protected]

i.e. In two different rows.

How to do so?

0

2 Answers 2

5

Use this:

SELECT 
    node.value('.','varchar(100)') AS Val
FROM
    @x.nodes('/PartnerEmails/Email') AS PE(Node)    

Since you have multiple nodes inside <PartnerEmails>, you need to use the .nodes() function to create an "inline" table of XML fragments - each "row" in that table contains one <Email> node which you can then query on (and extract the contents of the XML node).

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

Comments

1
DECLARE @x XML
SELECT @x = '<PartnerEmails>
<Email>[email protected]</Email>
<Email>[email protected]</Email>
</PartnerEmails>'

SELECT   ColumnValue.value('.','varchar(1000)')  as Val            
FROM @x.nodes('/PartnerEmails/Email') as Table1(ColumnValue) 

1 Comment

Great answer, as it provides both input data definition as well as recipe. @Asif, shall we update answer one and get rid of this one? It is nearly a duplicate, plus the first one has an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.