Good Morning All,
The other day I had to create a new function within SQL Server. I think this is pretty poweful and useful so I would like to share what I learned and did.
So what did I have do? I had to find a way to remove HTML from a string and output just the plain text. I needed to make sure to output the last whole word and not cut the output in the middle of a word. This function will use the following functions in SQL Server:
- Substring
- Charindex
- Stuff
- Reverse
- Ltrim
- Rtrim
--Remove table if its thereIF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N'tempdb..#HtmlText') ) DROP TABLE #HtmlText
--Create new tableCREATE TABLE #HtmlText(ID INT IDENTITY(10000000,1),String TEXT)
--insert valuesINSERT INTO #HtmlText(String )
SELECT '<html>
<head>
<title>HTML Generator Sample Page</title>
</head>
<body>
<h2>
HTML Generator</h2>
<p>
Use this HTML generator to generate HTML code for your own website or blog. Use the toolbar to:</p>
<ul>
<li>
Change the <span style="color:#800000;">text color</span></li>
<li>
Create <strong>bold</strong> or <em>italic</em> text.</li>
<li>
Change the <span style="color:#800000;">text color</span></li>
<li>
Change the <span style="font-family:comic sans ms,cursive;">font family</span> or <span style="font-size:12px;">font size</span></li>
<li>
Create <a href="http://www.html.am/html-codes/links/">hyperlinks</a></li>
<li>
Create a bulleted list...</li>
<li>
...and much more!</li>
</ul>
<p>
To view the source code, click on the "Source" button above (on the left).</p>
</body>
</html>'
UNION ALL
SELECT '<!-- Codes by HTML.am -->
<div style="width:150px;height:150px;line-height:3em;overflow:scroll;padding:5px;">
This ''div'' element uses ''overflow:scroll'' to create scrollbars whenever the contents of the ''div'' become too large.
</div>'
UNION ALL
SELECT '<!-- Codes by HTML.am -->
<p>Check out <a href="http://www.travel-explorer.com/rob-roy-glacier-in-new-zealand/">Rob Roy Glacier</a>!</p>'
Now let's jump into the function. I have two parameters in this function. The first is @HTMLText and the other is @Textlength. These pretty much define themselfs. @HtmlText is the html text you are passing into the function where as the @textLength is the lengh of text you want returned backed. We need not want the entire string returned so I created this parameter.
Before outputting the entire function I want to review a few important lines.
--Taking the first X amount of chars that include a full word
SET @HTMLText= SUBSTRING(@HTMLText,0,@TextLength-(CHARINDEX(' ',REVERSE(SUBSTRING(@HTMLText,0,@TextLength)))))
This line will return the last whole word. So here is an example.
Whole String: 'Johnny Bench made a wondeful catch on Sunday afternoon.'
Original Output: 'Johnny Bench made a wonderful ca'
Output with Substring: 'Johnny Bench made a wonderful'
As you can see it by be my helpful and meaningful.
The other import line I created was to remove ' " ' if its the the first char in the string. I talked about using the Stuff command in a previous blog entry. You can read it here to learn more indepth about it.
--Removing the " as the beginning of the string if its there.IF SUBSTRING(@HTMLText,1,1)='"' SET @HTMLText= STUFF(@HTMLText,1,1,'')
Now lets look at the function before doing some simple select statements.
create FUNCTION [dbo].[f_GetPlainTextSubstringFromHTML] (@HTMLText VARCHAR(MAX),@TextLength INT = NULL) RETURNS VARCHAR(MAX)AS
BEGIN
--variables
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
--assigning values to variables
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
IF @TextLength = NULL SET @TextLength = 50
--Removing HTML Text
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
--Taking the first X amoutn of chars that include a full word
SET @HTMLText= SUBSTRING(@HTMLText,0,@TextLength-(CHARINDEX(' ',REVERSE(SUBSTRING(@HTMLText,0,@TextLength)))))
--Removing the " as the beginning of the string if its there.
IF SUBSTRING(@HTMLText,1,1)='"' SET @HTMLText= STUFF(@HTMLText,1,1,'')
--Just triming the text before outputting it
RETURN LTRIM(RTRIM(@HTMLText))END
Let's see how this function works.SELECT String, dbo.f_GetPlainTextSubstringFromHTML(String,100) FROM #HtmlText
That should sum up my SQL entry for today. Have fun and keep learning!
Robert