Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 playing with xml(html)

Author  Topic 

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-12-22 : 22:52:40
[code]
declare @x xml
set @x =
'<table>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>100</td>
<td>200</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>300</td>
<td>400</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
'

/*desired output in table
col1 | col2
100 | 200
|
300 | 400
|*/

--failed query
select r.value('.','varchar(100)'), r1.value('.','varchar(100)')
from @x.nodes('table') as m(c)
cross apply m.c.nodes('tr') as x(r)
cross apply x.r.nodes('td') as x1(r1)
[/code]

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-12-23 : 08:03:39
[code]declare @x xml
set @x =
'<table>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>100</td>
<td>200</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>300</td>
<td>400</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
'

/*desired output in table
col1 | col2
100 | 200
|
300 | 400
|*/

SELECT n.value('(td[1])', 'VARCHAR(20)') AS Col1,
n.value('(td[2])', 'VARCHAR(20)') AS Col2
FROM @x.nodes('(/table/tr)') AS x(n)[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -