01.29.08

Problem With Nulls in XML generation

Posted in Important SQL Query, SQL Server, Utlities, xml tagged , , , , , at 12:18 pm by dayananthan

Suppose we have to display all the data in the table into an XML format and at that time we have some null values in the table. Sometime few peoples will face an issue that they want “NULL” to displayed in XML for the null data. So we can we use query like this to get it.

Code: 

SELECT

EmpID AS ‘empid’,

ISNULL(EmpName, ‘NULL’) AS ‘empname’

FROM Emp

FOR XML PATH(‘EmpDetails’), ROOT(‘Employees’); 

Output: 

<Employees>  <EmpDetails>    <empid>1</empid>    <empname>Dayaa</empname>  </EmpDetails>

  <EmpDetails>

    <empid>3</empid>

    <empname>NULL</empname>

  </EmpDetails>

</Employees>