2012-09-12 85 views
2

我在我的表<在SQL Server中轉換爲<

一些數據時,我選擇與此查詢數據

select TblActionHistories.Comments from TblActionHistories WHERE TblActionHistories.Entity=CAST('Task' AS VARCHAR) AND EntityId=CAST(32 AS VARCHAR) 

它的未來是這樣

Task- <b>T1</b> has been added by Swapnil Sharma 
Task- <b>T1</b> status changed to <b>In Progress</b> by Swapnil Sharma<br/> 

但現在罰款我想我的上述結果#分開使用stuff所以我使用這個

SELECT STUFF((SELECT '#' + ISNULL(CAST(TblActionHistories.Comments AS VARCHAR(MAX)),'') FROM TblActionHistories WHERE TblActionHistories.Entity=CAST('Task' AS VARCHAR) AND EntityId=CAST(32 AS VARCHAR) for xml path ('')),1,1,'') 

它給了我這個

Task- &lt;b&gt;T1&lt;/b&gt; has been added by Swapnil Sharma#Task- &lt;b&gt;T1&lt;/b&gt; status changed to &lt;b&gt;In Progress&lt;/b&gt; by Swapnil Sharma&lt;br/&gt; 

,你可以清楚地看到,所有的特殊字符。像<>分別

轉換爲&lt; &gt;請幫我有了這個,我希望他們來的原始格式

+0

然後你不應該把它們轉換成XML。所以你的問題應該是「什麼是連接行值,同時避免將文本轉換爲XML的好方法?」 –

+1

我相信這是因爲FOR XML,看看這篇文章:http://sqlblog.com/blogs/rob_farley/archive/2012/06/08/24296.aspx –

+1

謝謝@IvanG它的作品 – rahul

回答

4

嗯,我找到了解決方案通過IvanG

SELECT STUFF((SELECT '#' + ISNULL(CAST(TblActionHistories.Comments AS VARCHAR(MAX)),'') FROM TblActionHistories WHERE TblActionHistories.Entity=CAST('Task' AS VARCHAR) AND EntityId=CAST(32 AS VARCHAR) for xml path(''), root('MyString'), type).value('/MyString[1]','varchar(max)') ,1,1,'') 

參考建議。對這篇文章

http://sqlblog.com/blogs/rob_farley/archive/2012/06/08/24296.aspx

-2

嘗試鑄造NVARCHAR而非VARCHAR

SELECT STUFF((SELECT '#' + ISNULL(CAST(TblActionHistories.Comments AS NVARCHAR(MAX)),'') FROM TblActionHistories WHERE TblActionHistories.Entity=CAST('Task' AS NVARCHAR) AND EntityId=CAST(32 AS VARCHAR) for xml path ('')),1,1,'') 
+0

不,它不會工作 – rahul

相關問題