Convert HTML to Plain Text in SQL

Learn practical SQL methods to remove HTML markup and return readable plain text from stored HTML content.

Before you start

This task runs inside your database engine, not in a separate file utility or web service. You need a value or column that contains HTML and a SQL client that can run functions on string data. The exact function names vary by database: PostgreSQL, MySQL, SQL Server, Oracle, SQLite, BigQuery, Snowflake, Redshift, and other engines do not share one built-in HTML-to-text converter. Check which engine and version you are using before choosing an approach.

For a one-time cleanup, a query is enough. For repeated imports, logs, CMS exports, or form data, place the conversion in a view, a generated column, or an update statement. Always work on a copy or inside a transaction when updating existing rows so you can roll back if the output loses important content.

Step-by-step instructions

Start with a simple regex replacement when your HTML is predictable, such as plain paragraphs, bold, italics, or links without nested scripts or styles. The goal is to remove tags while keeping the visible text.

MySQL

MySQL has no native strip_tags function, but its regular-expression functions can handle basic cases. In MySQL 8, REGEXP_REPLACE accepts a match string:

  • Remove common tag pairs with REGEXP_REPLACE(html, '<[^>]+>', '').
  • Decode common entities such as &, <, >, and ' with REPLACE calls.
  • Trim and collapse extra whitespace using TRIM and one or more REGEXP_REPLACE operations.

Earlier MySQL versions before 8.0 do not provide REGEXP_REPLACE. In that case, use a custom stored function, a preprocessing step outside the database, or an application-side library.

PostgreSQL

PostgreSQL supports REGEXP_REPLACE natively. A common expression is:

  • Use REGEXP_REPLACE(html, '<[^>]*>', '', 'g') to remove tags.
  • Use REGEXP_REPLACE(..., '\s+', ' ', 'g') to collapse runs of whitespace.
  • Use REPLACE or a small function to convert the entities you expect in your content.

Keep the conversion inside a SELECT query for read-only reporting. To store the cleaned text, put the expression in an UPDATE or a generated column and keep the original HTML intact.

SQL Server

SQL Server does not include regex replacement in T-SQL by default. For values without script or style blocks, one practical method is to repeatedly remove characters between angle brackets by position.

  • Locate the first '<' with CHARINDEX and the first '>' after it.
  • Remove that substring with STUFF.
  • Repeat until no '<' remains.
  • Clean up  , &, <, and > with REPLACE.

Because a pure T-SQL loop can be slow on large tables, restrict this kind of processing to selected rows, a staging table, or a batch update. SQL Server 2022 and Azure SQL offer additional functions, but the basic position-based method works across many versions.

Oracle, SQLite, BigQuery, Snowflake, and Redshift

Some engines provide a regular-expression match or replace function, while others only offer limited string functions:

  • Oracle has REGEXP_REPLACE and can use '<[^>]*>' as a pattern, but check available metacharacters for your Oracle version.
  • SQLite has no native REGEXP_REPLACE in most builds and usually requires an application-defined function or a custom extension.
  • BigQuery has REGEXP_REPLACE with RE2 syntax; the pattern should use a non-greedy form such as r'<[^>]*>' or another pattern valid for RE2.
  • Snowflake has REGEXP_REPLACE and supports a parameters argument, such as '<[^>]*>' with the global replacement flag.
  • Redshift supports REGEXP_REPLACE with POSIX-style patterns in many cases; confirm the exact syntax for your cluster version.

Do not assume a MySQL or PostgreSQL expression will run unchanged on another engine. Version and regex flavor matter, especially for entity decoding, nested tags, comments, scripts, or multi-line content.

Check the output

Compare the original HTML and the converted text side by side on a small sample before updating a large table. Confirm that visible text is present, tag markup is removed, and common entities are readable. Look for missing words caused by a pattern stopping too early, and for phrases that run together because a tag was removed without adding a space.

A simple tag-stripping expression does not distinguish visible text from script or style content. If the source contains JavaScript or CSS, those contents will remain in the result. Test with input that includes links, images, headings, paragraphs, and an entity such as & or '. If the output contains leftover '>' characters, script text, or unreadable entity codes, the conversion needs a stricter pattern or a post-processing step.

Common problems and next steps

HTML entities remain

Tag removal does not automatically decode &, ",  , or numeric references. Add targeted REPLACE calls for the entities present in your data, including the numeric forms you actually see. Do not apply every possible entity unless your dataset contains it, because additional replacements may change text unintentionally.

Spaces are missing or text runs together

When tags are removed from markup such as '<td>A</td><td>B</td>', the result becomes 'AB' instead of 'A B'. Replace block-level boundaries with a space before removing remaining tags, or add whitespace normalization after tag removal. A boundary pattern depends on the tags in your source.

Script or style content appears in the result

Remove '<script>...</script>' and '<style>...</style>' blocks before the general tag removal if those blocks are present. A simple '<[^>]*>' match does not capture the content between those tags, so JavaScript and CSS survive as visible text.

Malformed HTML breaks the pattern

Real-world HTML may include unclosed tags, comments, attributes with '>' inside quoted values, or case differences. If the data is not clean, a single SQL expression becomes difficult to maintain. Consider cleaning or parsing in a small script and storing the plain-text result before it enters the database, or a database extension designed for HTML parsing. SQL alone may be enough for predictable fragments, but it is not a full HTML parser.