November 18, 2014 07:55 by
Peter
I have been focusing on SQL Reporting Service (SSRS) 2012 using a supply database that had terribly fascinating worth inside the field. It looked such as HTML upon the website :
It is very annoying for business users to discover some thing similar to this on SSRS report :
SELECT TheName = 'Put your <span style="color: #ff0000; font-weight: bold; text-decoration: underline">name </span>in the box:'
This was a " name " column simply beneath the ID in database and there wasn't some other columns along with no HTML. I'm not so positive in case somebody took a incorrect approach making this field using HTML or that was an intension of the developer creating his life easier on the online interface, however I've determined to get yourself a quick answer to eliminate it and to provide the report when I can.
CREATE FUNCTION fn_RemoveHTMLFromText (@inputString nvarchar(max))
RETURNS nvarchar(MAX)
AS
BEGIN
/*Variables to store source fielde temporarily and to remove tags one by one*/
DECLARE @replaceHTML nvarchar(2000), @counter int, @outputString nvarchar(max)
set @counter = 0
SET @outputString = @inputString
/*This was extra case which I've added later to remove no-break space*/
SET @outputString = REPLACE(@outputString, ' ', '')
/*This loop searches for tags beginning with "<" and ending with ">" */
WHILE (CHARINDEX('<', @outputString,1)>0 AND CHARINDEX('>', @outputString,1)>0)
BEGIN
SET @counter = @counter + 1
/*
Some math here... looking for tags and taking substring storing result into temporarily variable, for example "</span>"
*/
SELECT @replaceHTML = SUBSTRING(@outputString, CHARINDEX('<', @outputString,1), CHARINDEX('>', @outputString,1)-CHARINDEX('<', @outputString,1)+1)
/* Replace the tag that we stored in previous step */
SET @outputString = REPLACE(@outputString, @replaceHTML, '')
/* Let's clear our variable just in case... */
SET @replaceHTML = ''
/* Let's set up maximum number of tags just for fun breaking the loop after 15 tags */
if @counter >15
RETURN(@outputString);
END
RETURN(@outputString);
END
And now, let use the fuction:
SELECT TheName = dbo.fn_RemoveHTMLFromText ('Put your <span style="color: #ff0000; font-weight: bold; text-decoration: underline">name </span>in the box:')
And finally, this is what I wanna see:
This is simply easy resolution that has been applied upon the field along with not so many HTML tags, other then may be simply changed into more serious HTML cleaner.