SQL Server UrlEncode
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ UrlEncode] ') and xtype in (n ' FN ', n ' IF ', n ' TF ')--delete the function drop functions [dbo]. [UrlEncode] Gocreate FUNCTION dbo. UrlEncode (@URL varchar (3072)) RETURNS varchar (3072) asbegin DECLARE @count int, @char varchar (2), @i int, @bytes binary (2), @low8 int, @high8 intdeclare @URLEncode varchar (3000); Set @count = LEN (@URL) Set @i = 1SET @URLEncode = "while (@i <= @count) BEGIN SET @char = SUBSTRING (@URL, @i,1) IF @char Like ' [-a-za-z0-9 () ' *._!] ' and datalength (@char) = 1 Set @URLEncode = @URLEncode + @char ELSE begin IF Datalength (@char) = 1 begin SET @URLEncod E = @URLEncode + '% ' + SUBSTRING (' 0123456789ABCDEF ', (ASCII (@char)/+ 1), 1) SET @URLEncode = @URLEncode + SUBSTRING (' 0123456789ABCDEF ', (ASCII (@char)% + 1), 1) END ELSE BEGIN Set @bytes = CONVERT (binary, @char) Set @high8 = (0xff00 & Cast (@bytes as int))/256 Set @low8 = 0x00FF & CAST (@bytes as int) Set @URLEncode = @URLEncode + '% ' + SUBST RING (' 0123456789ABCDEF ', (@high8/16 + 1), 1) Set @URLEncode = @URLEncode + SUBSTRING (' 0123456789ABCDEF ', (@high8% + 1), 1) Set @URLEncode = @URLEncode + '% ' + SUBSTRING (' 0123456789ABCDEF ', (@low8/16 + 1), 1) SET @URLEncode = @URLEncode + SUBSTRING (' 0123456789ABCDEF ', (@low8% 16 + 1), 1) End end SET @i = @i + 1 END RETURN @URLEncode; END
SQL Server UrlEncode