欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Sqlserver UrlEncode

程序员文章站 2022-10-04 23:43:12
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