Convert HTML to Plain Text in SQL Server

Practical ways to convert HTML to plain text in SQL Server, with T-SQL examples, output checks, and common fixes for entities, tags, and whitespace.

Before you start

SQL Server does not have a built-in function named something like HTML-to-text, so the practical choices are to handle the conversion in T-SQL or to prepare the text outside the database before storing or comparing it. T-SQL can remove common tags and decode common entities, but it is not an HTML parser. It will not correctly interpret every possible document structure, CSS rule, or malformed fragment.

Check where the HTML comes from and where the result will be used. If the conversion only needs to run on one value or a small set, a T-SQL expression may be enough. If the data is large or must preserve exact rendered text, a service or application-side parser that can call SQL Server is usually a better fit. This article covers a T-SQL approach that works locally in the database.

Step-by-step instructions

Start with a copy of the value so the original HTML remains available if you need to inspect or restore it later.

First remove script and style blocks, because their content is normally not part of the visible text. A common pattern is to use a function that repeatedly removes the content between paired tags. For example, you can call a user-defined function that replaces everything from an opening tag to its matching closing tag with an empty string.

Next remove the remaining tags. A simple approach uses REPLACE or a loop that finds each less-than and greater-than pair and removes that range. This removes attribute text, links, image elements, and formatting tags.

Then decode common character entities. Replace non-breaking space entities with a space, and convert entities such as ampersand, quote, apostrophe, less-than, and greater-than to their plain characters. Numeric entity decoding requires additional handling and may need application code if the data contains many numeric forms.

Finally clean the spacing. Collapse repeated spaces, tabs, and line breaks to single spaces or to a chosen separator, then use LTRIM and RTRIM to remove leading and trailing whitespace.

To keep the expression maintainable, wrap the logic in a function or a reusable query. During development, run each step separately on one sample value so you can see what was removed and what remains.

Check the output

Compare the result with a known sample, not with a guess about what the HTML should produce. Keep a small set of inputs where the expected plain text is already confirmed, including a paragraph, a list, an entity, and an embedded script or style block.

Confirm that visible text remains, tags and script or style content are gone, common entities are decoded, and whitespace is readable. The result will often lose block boundaries such as paragraph or list separation unless you add explicit replacements for those elements before the tag-removal step.

A plain T-SQL removal routine may leave punctuation attached incorrectly, merge words from separate HTML elements, or keep text that was hidden only by CSS. If those details matter, treat the database result as approximate and verify important output outside SQL Server.

Common problems and next steps

If entity text remains, the expression may not cover the entity forms present in the data. Check whether the values use named entities, decimal numeric entities, or hexadecimal numeric entities, and add handling for the forms that actually appear.

If text from script or style blocks appears in the output, remove those blocks before removing ordinary tags. If words run together, replace block-level tags with spaces or line breaks before the general tag-removal step.

If malformed HTML gives inconsistent results, use a parser rather than continuing to add T-SQL cleanup rules. SQL Server can send the value to an application, CLR procedure, or external service that parses the HTML and returns the plain text. Verify privacy and file-handling requirements for that path before moving data outside the database.

When the conversion will be used in search or matching, normalize both the source and the search text the same way, including case, entities, and whitespace. That makes the result more useful than relying on the raw HTML comparison.