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

 

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐