Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In SQL im writing a query to create a xml

SQL
Select
Case When (A.Course_Fee,0)=0 then 0 else Ceiling (A.Course_Fee) end),2,1) as "ABC/DEF"
From Tab1 B inner join Tab1 A on A.Sno = B.Sno where B.Sname = 'AB'


I want to remove/delete the tag DEF if Course_Fee = 0

Can you please provide the solution?
Posted
Updated 28-Aug-13 20:05pm
v2

1 solution

SQL
Declare @XML XML
Set @XML ='
<row>
  <ABC>
    <DEF>1</DEF>
  </ABC>
</row>
<row>
  <ABC>
    <DEF>0</DEF>
  </ABC>
</row>'

SELECT line.DEF.value('ABC[1]', 'Int') as ABC
FROM @XML.nodes('row') AS line(DEF) 
--Where line.DEF.value('ABC[1]','Int') <>0 -- if you don't want to see 0 Un-comment it

--Output will be
--ABC
-----
--1
--0

SET @XML.modify('delete //row/ABC/DEF [.=''0'']')
--Output will be
--<row>
--  <ABC>
--    <DEF>1</DEF>
--  </ABC>
--</row>
--<row>
--  <ABC />
--</row>


SELECT line.DEF.value('ABC[1]', 'Int') as ABC
FROM @XML.nodes('row') AS line(DEF) 

--Output will be
--ABC
-----
--1
--0
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900