Reputation: 7663
i have some data in my table
when i select that data with this query
select TblActionHistories.Comments from TblActionHistories WHERE TblActionHistories.Entity=CAST('Task' AS VARCHAR) AND EntityId=CAST(32 AS VARCHAR)
it's coming fine like this
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/>
but now i want my above result # separated using stuff
so i am using this
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,'')
it gives me this
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/>
you can clearly see that all the special char. like < > converted to < >
respectively
please help me out with this i want them to come in their original format
Upvotes: 3
Views: 11951
Reputation: 7663
well i found the solution as suggested by 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,'')
ref. to this article
http://blogs.lobsterpot.com.au/2010/04/15/handling-special-characters-with-for-xml-path/
Upvotes: 6
Reputation: 69
Try casting to NVARCHAR instead of 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,'')
Upvotes: -2