Sqlserver UrlEncode
Sqlserver UrlEncode
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UrlEncode]') and xtype in (N'FN', N'IF', N'TF')) -- 删除函数 drop function [dbo].[UrlEncode] GO CREATE FUNCTION dbo.UrlEncode(@URL varchar(3072)) RETURNS varchar(3072) AS BEGIN DECLARE @count int, @char varchar(2), @i int, @bytes binary(2), @low8 int, @high8 int DECLARE @URLEncode varchar(3000) ; SET @count = LEN(@URL) SET @i = 1 SET @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 @URLEncode = @URLEncode + '%' + SUBSTRING('0123456789ABCDEF',(ASCII(@char) / 16 + 1),1) SET @URLEncode = @URLEncode + SUBSTRING('0123456789ABCDEF',(ASCII(@char) % 16 + 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 + '%' + SUBSTRING('0123456789ABCDEF',(@high8 / 16 + 1),1) SET @URLEncode = @URLEncode + SUBSTRING('0123456789ABCDEF',(@high8 % 16 + 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