Original: SQL to the URL encoding to Chinese characters!
--=============================================--Author: ruijc--Description: Convert URL encoding to clear text string--================================ =============create FUNCTION Fn_urldecode (@Str varchar (8000)--encoded string) RETURNS varchar (8000) Asbegin DECLARE @ Position INT; --the '% ' character is located DECLARE @Chr CHAR (16); --character constant DECLARE @Pattern CHAR (21); DECLARE @ParseStr VARCHAR (8000);--The decoded string DECLARE @Hex uniqueidentifier;--define the 16 binary template because the GUID is conveniently converted to byte DECLARE @CurrWord in t;--current word DECLARE @BitsCount INT;--current decoding bit DECLARE @HightByte tinyint;--high byte DECLARE @LowByte tinyint;--low byte /**************** variable initialization ***********************/SET @Chr = ' 0123456789abcdef '; SET @Pattern = '%[%][a-f0-9][a-f0-9]% '; SET @[email protected]; SET @Hex = ' 00000000-0000-0000-0000-000000000000 '; SET @CurrWord = 0; SET @BitsCount = 0; SET @HightByte = 0; SET @LowByte = 0; IF (@Str is not NULL OR @Str <> ") BEGIN SET @Position = PATINDEX (@Pattern, @ParseStr);--Get firstA '% ' location while @Position >0 BEGIN SET @Hex =stuff (@Hex, 7,2,left (@ParseStr, Len (@ParseStr)-@Positi On), 2)); SET @HightByte =cast (CAST (@Hex as BINARY (1)) as INT); IF (@HightByte & [email protected]) Begin--ascii code is converted directly to UTF-8 or UTF-16 SET @[email protected]; SET @BitsCount = 1; END IF (@HightByte & 192=192) Begin--unicode encoded SET @[email protected] & 31 ; SET @BitsCount = 2; END IF (@HightByte & 224=224) begin--utf-8 encoded Set@currword = @HightByte & 15SET @BitsCount = 3 END IF (@HightByte & 240=240) begin--utf-16 encoded Set@currword = @HightByte & 7SET @BitsCount = 4 END DECLARE @Index INT; DECLARE @NEWCHAR NVARCHAR (2); SET @Index = 1; SET @NEWCHAR = '; While @Index < @BitsCount BEGIN IF (LEN (@ParseStr) [email protected]* @Index) <0 BEGIN SET @[email protected]; SET @Position = 0; break; Endset @NEWCHAR = Left (right (@ParseStr, LEN (@ParseStr)-@Position -3* @Index), 2); --If there is no 16 encoding, break the outer whileif @NEWCHAR not LIKE ' [a-f0-9][a-f0-9] ' beginset @ParseStr = @StrSET @Position =0;--interrupt outer whilebreak ; END Set @Hex = STUFF (@Hex, 7, 2, @NEWCHAR) SET @LowByte = cast (CAST (@Hex as BINARY (1)) as INT); IF @LowByte &192=192begin SET @ParseStr = @StrSET @Position =0;--interrupts the outer whilebreak; END SET @CurrWord = (@CurrWord * 64) | (@LowByte & 63) SET @Index [email protected]+ 1 END IF @BitsCount > 1 SET @ParseStr = STUFF (@ParseStr, @Position, (@BitsCount), NCHAR (@CurrWord)) ELSE Beginset @ParseStr = STUFF (@ParseStr, @Position, 2, NCHAR (@CurrWord)) Set @ParseStr = STUFF (@ParseStr, @Position +1, 1, N ") END----get the next '% ' location The position SET @Position = PATINDEX (@Pattern, @ParseStr); End End RETURN @ParseStr; Endgoselect dbo. Fn_urldecode ('%e4%bd%a0%e6%98%af%e5%93%aa%e4%b8%aa ')
SQL to encode URL to kanji!