Sqlserver UrlEncode

Posted LittleFeiHu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Sqlserver UrlEncode的主要内容,如果未能解决你的问题,请参考以下文章

sql查询中怎么实现 asp的server.urlencode的功能,把汉字编码成%CD%D3的形式

缺少 SQL SERVER 2014 代码片段

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

php如何用urlencode()我下面所写的这段代码进行中文加密?

使用 Base64UrlEncode 的语句 [关闭]

sqlserver-处理死锁