2

我的表中有一些数据

当我使用此查询选择该数据时

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;像<>分别转换成

请帮我解决这个问题,我希望他们以原始格式出现

4

2 回答 2

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://blogs.lobsterpot.com.au/2010/04/15/handling-special-characters-with-for-xml-path/

于 2012-09-12T07:18:16.720 回答
-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,'')
于 2012-09-12T07:14:24.713 回答