How to convert a string to a string encoded in UTF-8 and vice versa?

A has a string %c3%adthat is decoded using UTF-8, does matter Γ­, but is decoded with ASCII Γƒ.

I need to decode it using UTF-8 encoding, how can I do this?

Here is the choice of meaning and what it should be ...

SELECT
('%c3%81') as 'Á (81 = 129)',
('%c3%89') as 'Γ‰ (89 = 137)',
('%c3%8d') as 'Í (8d = 141)',
('%c3%93') as 'Γ“ (93 = 147)',
('%c3%9a') as 'Ú (9a = 154)'


SELECT
('%c3%a1') as 'Γ‘ (a1 = 161)',
('%c3%a9') as 'Γ© (a9 = 169)',
('%c3%ad') as 'Γ­ (ad = 173)',
('%c3%b3') as 'Γ³ (b3 = 179)',
('%c3%ba') as 'ΓΊ (ba = 186)'
+5
source share
1 answer

These functions seem to do the job.

CREATE FUNCTION [dbo].[UrlDecodeUTF8](@URL varchar(3072))
RETURNS varchar(3072)
AS
BEGIN 
    DECLARE @Position INT,
        @Base CHAR(16),
        @Code INT,
        @Pattern CHAR(21)

    SELECT @URL = REPLACE(@URL, '%c3', '')

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

    WHILE @Position > 0
        SELECT @Code = Cast(CONVERT(varbinary(4), '0x' + SUBSTRING(@URL, @Position + 1, 2), 1) As int),
            @URL = STUFF(@URL, @Position, 3, NCHAR(@Code + 64)),
            @Position = PATINDEX(@Pattern, @URL)

    RETURN REPLACE(@URL, '+', ' ')

END
+1
source

All Articles