如何在sql这个例子中的结果xquery中添加分隔符?

时间:2016-11-22 12:01:55

标签: sql-server tsql xquery

如何在sql这个例子中的一个单元格之间添加分隔符(,)?

declare @htmlXML xml = N'<td valign="top" style="border-bottom:1px dashed;"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td valign="bottom"  align="left" style=" font:bold 11px Verdana, Arial, Helvetica, sans-serif;color:#3B4F89; border-bottom:double #3B4F89;">ACER Aspire 1300 Series Laptop Accessory Information:            </td></tr><tr><td class="main"><b>Specification:</b> Brand New ACER Aspire 1360 Series / TravelMate 240, 250 Series laptops CPU fanTested to be 100% working properly. <b>Unit:</b> PCS <b>Type:</b> Laptop CPU Fan<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition"/><b>Condition:</b> Brand New<b>Warranty:</b> 3 Months<b>Power:</b> DC5V 0.4A<b>Info:</b> Size(mm): 61 x 61 x 12.4, Wire Length: 68mm, (3-wire)3-pin connector</td></tr>            <tr><td class="main"><b>Availability:&amp;nbsp;</b><meta itemprop="availability" content="http://schema.org/InStock"/>In Stock</td></tr><tr><td class="main"><b>Payment | Delivery:&amp;nbsp;</b>PayPal | HongKong Registered Air Mail With Tracking Number, Free&amp;nbsp;&amp;nbsp;<a class="resources-newproduct" href="product_info.php/products_id/3840/vAspire+1300+Series#bottom" title="Jump to detail of payment and shipping">[Detail?]</a></td></tr><tr><td height="30" align="left" valign="middle"><a href="https://www.battery-adapter.com/product_info.php/products_id/3840/action/buy_now/products_id/3840"><img src="includes/languages/english/images/buttons/button_in_cart.gif" border="0" alt="Add to Cart" title=" Add to Cart " width="93" height="24"/></a>             </td></tr><tr><td><!-- tell_a_friend //--><table border="0" width="100%" cellspacing="0" cellpadding="1" class="infoBox"><tr><td><table border="0" width="100%" cellspacing="0" cellpadding="0" class="infoBoxContents"><tr><td><img src="images/pixel_trans.gif" border="0" alt="" width="100%" height="1"/></td></tr><tr><td align="left" class="boxText"><form name="tell_a_friend" action="https://www.battery-adapter.com/tell_a_friend.php" method="get"><a class="index_newlink" href="https://www.battery-adapter.com/product_reviews.php/products_id/3840"><img class="image_float" alt="View &amp; Write Reviews" src="includes/languages/english/images/buttons/button_write_view.gif"  border="0"/></a><a class="index_newlink" href="https://www.battery-adapter.com/ask_question.php/products_id/3840"><img class="image_float" alt="Ask a question" src="includes/languages/english/images/buttons/button_ask_question.gif"  border="0"/></a></form></td></tr><tr><td><img src="images/pixel_trans.gif" border="0" alt="" width="100%" height="1"/></td></tr></table></td></tr></table>'
    ;


SELECT t.v.value('.','nvarchar(max)') as b
FROM   @htmlXML.nodes('td[2]/table/tr[2]/td') as t(v)

所需的输出:

Specification: Brand New ACER Aspire 1360 Series / TravelMate 240, 250 Series laptops CPU fan Tested to be 100% working properly. 
 , Unit: PCS 
, Type: Laptop CPU Fan
, Condition: Brand New
, Warranty: 3 Months
, Power: DC5V 0.4A
, Info: Size(mm): 61 x 61 x 12.4, Wire Length: 68mm, (3-wire)3-pin connector

1 个答案:

答案 0 :(得分:1)

使用CTE和变量:

declare @htmlXML xml = N'<td></td><td><table><tr></tr><tr><td class="main"><b>Specification:</b> Some item specification <b>Unit:</b> PCS <b>Type:</b> Laptop CPU Fan<meta itemprop="itemCondition"/><b>Condition:</b> Brand New<b>Warranty:</b> 3 Months<b>Power:</b> DC5V 0.4A<b>Info:</b> Size(mm): 61 x 61 x 12.4, Wire Length: 68mm, (3-wire)3-pin connector</td></tr></table></td>';

declare @Info nvarchar(max);

with CTE AS (
    SELECT 
    row_number() over (order by (select 0)) as rn,
    cast(t.v.query('.') as varchar(max)) as value 
    FROM @htmlXML.nodes('td[2]/table/tr[2]/td//text()') as t(v)
)
select @Info = concat(@Info + char(13) + ', ',t1.value , rtrim(t2.value))
from CTE t1 
join CTE t2 on (t1.rn = t2.rn-1)
where t1.rn%2 > 0;

select @Info;