strip HTML tags in in SQL Server

Is there a way to strip HTML (Rich Text actually) from a SQL Server column in a query?

I've inherited a SQL Server database with an Access frontend.  Users have been allowed to edit a memo (long text) fields using Rich Text format, which leaves Rich Text (Div and font) tags embedded in the fields.  In Access, I can strip these tags using the PlainText function, but I'm working on editing a report in SSRS, and need to strip these tags using a SQL Server function.
LVL 52
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try

We need to write a function to strip the HTML characters.

CREATE FUNCTION [dbo].[udf_StripHTML] (@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<',@HTMLText) SET @End = 
CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText)) 
SET @Length = (@End - @Start) + 1 WHILE @Start > 0
AND @End > 0
AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
SET @Start = CHARINDEX('<',@HTMLText) SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
END
RETURN LTRIM(RTRIM(@HTMLText))
END
GO

Open in new window


Trial.


SELECT dbo.udf_StripHTML('<b>Pawan </b>
 
<a href="Rames">heelo</a>')


OUTPUT
(No column name)
Pawan
 
heelo

https://blog.sqlauthority.com/2007/06/16/sql-server-udf-user-defined-function-to-strip-html-parse-html-no-regular-expression/
0
Pawan KumarDatabase ExpertCommented:
So we can simple xml query...

--

DECLARE @xml AS XML = '<b>Pawan </b> <a href="Rames">heelo</a> <html> I am ahere also </html>'
select @xml.query('for $x in /. return ($x)//text()') a

--

Open in new window


OUTPUT

/*------------------------
OUTPUT
------------------------*/
a
------------------------------
Pawan heelo I am ahere also 

(1 row(s) affected)

Open in new window

ref
0
Pawan KumarDatabase ExpertCommented:
Please try full complete solution

--

CREATE TABLE StripHTMLtags ( ID INT , ht VARCHAR(MAX) ) 
GO

INSERT INTO StripHTMLtags VALUES 
(1,'<b>Pawan </b> <a href="Rames">heelo</a> <html> I am ahere also </html>'),
(2,'<b>Pawan </b> <a href="Rames">heelo</a>'),
(3,'Ramesh Krishna')
GO

--

Open in new window


SOLUTION

--

SELECT ID, CAST(ht AS XML).query('for $x in /. return ($x)//text()')
FROM StripHTMLtags

--

Open in new window


OUTPUT

--

/*------------------------
SELECT ID, CAST(ht AS XML).query('for $x in /. return ($x)//text()')
FROM StripHTMLtags
------------------------*/
ID          
----------- ------------------------------
1           Pawan heelo I am ahere also 
2           Pawan heelo
3           Ramesh Krishna

(3 row(s) affected)

--

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Snarf0001Commented:
The first solution by @Pawan should work properly.

You should not use the xml versions.  Unless your editor is using xhtml, and even then unless it's VERY tight about enforcing it, the resulting html will often not be compliant xml, and will cause errors.
Most editors I've used will not return valid / strict xml.
0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Thanks, appreciate the solution and the backup comments
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.