Search code examples
sql-serverxmlt-sqlfor-xml

Don't want the excess tag in my FOR XML Path('td')


I'm looking for an XML output like this:

<td id="1">123</td>

My current SQL statment:

SELECT EmpID AS "@ID", EmpNo FROM Employees FOR XML PATH('td')

Which gets me this:

<td id="1"><EmpNo>123</EmpNo></td>

All I'm looking to do is remove the EmpNo tag as it is not needed. I'm sure there is a very simple method for doing this but for the life of me I can't find it. I think I've been searching too long... time for a coffee.

Any help would be greatly appreciated.

JT...


Solution

  • You can do what you want like this:

    SELECT EmpID "td/@id", EmpNo "td" 
    FROM Employees 
    FOR XML PATH('')