码迷,mamicode.com
首页 > 数据库 > 详细

Sqlserver UrlEncode

时间:2018-08-29 13:57:55      阅读:867      评论:0      收藏:0      [点我收藏+]

标签:object   ike   class   string   lse   abc   ret   creat   byte   

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

 

Sqlserver UrlEncode

标签:object   ike   class   string   lse   abc   ret   creat   byte   

原文地址:https://www.cnblogs.com/LittleFeiHu/p/9552673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!