Gokul's Blog

SQL Proc to decode HTML(including Unicode Characters)

Leave a comment

One of my Co-worker sent a note about an interesting Stored procedure he wrote for decoding HTML content. I thought this would be helpful at some point for me/someone.

If you have any questions,please contact the author at the email mentioned below.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- EXEC UrlDecode '%c3%a1'
-- EXEC UrlDecode '%4D'

Create PROCEDURE UrlDecode
    -- Add the parameters for the stored procedure here
    @URL varchar(2000)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @Position INT,
        @Base CHAR(16),
        @High TINYINT,
        @Low TINYINT,
        @NewHigh INT,
        @Pattern CHAR(21)

    SELECT    @Base = '0123456789abcdef',
        @Pattern = '%[%][0-9a-f][0-9a-f]%',
        @Position = PATINDEX(@Pattern, @URL)

    SELECT    @High = CHARINDEX(SUBSTRING(@URL, @Position + 1, 1), @Base) -1,
            @Low = CHARINDEX(SUBSTRING(@URL, @Position + 2, 1), @Base) -1

    WHILE @Position > 0
    BEGIN

            IF (@High & 15) = 12 -- xC0 
            BEGIN
                SELECT @NewHigh = @Low * POWER(2, 6) --Shift Low 6 bits

                SELECT @High = CHARINDEX(SUBSTRING(@URL, @Position + 4, 1), @Base) -1
                SELECT @Low = CHARINDEX(SUBSTRING(@URL, @Position + 5, 1), @Base) -1

                SELECT @URL = STUFF(@URL, @Position, 6, CHAR(@NewHigh | (16 * @High) | @Low))

                SELECT @High = 0, @Low = 0, @NewHigh = 0

            END
            ELSE
            BEGIN
                SELECT @URL = STUFF(@URL, @Position, 3, CHAR(16 * @High | @Low))

            END

            SELECT @Position = PATINDEX(@Pattern, @URL)
            IF @Position > 0
            BEGIN
                SELECT    @High = CHARINDEX(SUBSTRING(@URL, @Position + 1, 1), @Base) -1,
                        @Low = CHARINDEX(SUBSTRING(@URL, @Position + 2, 1), @Base) -1
            END
    END

    SELECT @URL

END
GO

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

You can contact him at Gordon’s Email (remove 3-AT’s)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s