Monday, February 7, 2011

Function to convert Html code to proper character.

Input   : it's where ' is a html code of '(oppostrope).
Output : it'z

ALTER FUNCTION [dbo].[HTMLDecode]
(@txt NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE @Start INT
DECLARE @HTMLCode NVARCHAR(5)
DECLARE @DecodeTxt NVARCHAR(4000)
SET @DecodeTxt = @txt
SET @Start = 32
WHILE @Start<=126
BEGIN
SET @HTMLCode = '&#' + CAST(@Start AS NVARCHAR(5))+';'
SELECT @DecodeTxt=REPLACE(@DecodeTxt,@HTMLCode,CAST(NCHAR(@Start) AS NVARCHAR(5)))
SET @Start = @Start + 1
END
RETURN @DecodeTxt
END

Note:
From 32  to 126 is the ascii character number so for each there is a equvilent character.
For each the character between 32 to 126 there is html value starting as &#32; to &#126; what we are doing is we are replacing this html value to the equivalent character.

For any questions you can mail me..







No comments:

Post a Comment