0

I have the following table that holds a column which is in XML:

Id Label Details
1 Test1 <terms><destination><email>[email protected]</email><email>[email protected]</email></destination><content>blabla</content></terms>
2 Test2 <terms><destination><email>[email protected]</email><email>[email protected]</email></destination><content>blabla</content></terms>

I would like a query that produces the following output:

Any clue on how I can concat the XML email node values as a column along the related columns (Id and Label)? Thanks ahead

2 Answers 2

2
select ID, Label,
    stuff(
        details.query('for $step in /terms/destination/email/text() return concat(", ", string($step))')
        .value('.', 'nvarchar(max)'),
    1, 2, '')
from @tbl;
Sign up to request clarification or add additional context in comments.

Comments

1

Please try the following solution.

Because DDL and sample data population were not provided, assumption is that the Details column is of XML data type.

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, Label VARCHAR(20), Details XML);
INSERT INTO @tbl (Label, Details) VALUES
('Test1',N'<terms><destination><email>[email protected]</email><email>[email protected]</email></destination><content>blabla</content></terms>'),
('Test2',N'<terms><destination><email>[email protected]</email><email>[email protected]</email></destination><content>blabla</content></terms>');
-- DDL and sample data population, end

SELECT ID, Label 
    , REPLACE(Details.query('data(/terms/destination/email/text())').value('.','VARCHAR(MAX)'), SPACE(1), ', ') AS Destination
FROM @tbl;

Output

+----+-------+----------------------------------+
| ID | Label |           Destination            |
+----+-------+----------------------------------+
|  1 | Test1 | [email protected], [email protected] |
|  2 | Test2 | [email protected], [email protected] |
+----+-------+----------------------------------+

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.