TSQL - 如何URL编码

时间:2010-05-25 13:46:05

标签: sql sql-server tsql

寻找一个免费测试的sql脚本,我可以在UDF中使用它来编码通过sql的url。函数将接收URL并传递URL编码的URL。我见过一些,但我遇到的所有东西似乎都有一些缺陷。

5 个答案:

答案 0 :(得分:6)

Peter DeBetta的这个怎么样:

CREATE FUNCTION dbo.UrlEncode(@url NVARCHAR(1024))
RETURNS NVARCHAR(3072)
AS
BEGIN
    DECLARE @count INT, @c NCHAR(1), @i INT, @urlReturn NVARCHAR(3072)
    SET @count = LEN(@url)
    SET @i = 1
    SET @urlReturn = ''    
    WHILE (@i <= @count)
     BEGIN
        SET @c = SUBSTRING(@url, @i, 1)
        IF @c LIKE N'[A-Za-z0-9()''*\-._!~]' COLLATE Latin1_General_BIN ESCAPE N'\' COLLATE Latin1_General_BIN
         BEGIN
            SET @urlReturn = @urlReturn + @c
         END
        ELSE
         BEGIN
            SET @urlReturn = 
                   @urlReturn + '%'
                   + SUBSTRING(sys.fn_varbintohexstr(CAST(@c AS VARBINARY(MAX))),3,2)
                   + ISNULL(NULLIF(SUBSTRING(sys.fn_varbintohexstr(CAST(@c AS VARBINARY(MAX))),5,2), '00'), '')
         END
        SET @i = @i +1
     END
    RETURN @urlReturn
END

答案 1 :(得分:4)

我个人会在应用程序而不是数据库中执行此操作 - 但如果由于某种原因而必须启用CLR集成,那么这将是CLR UDF的完美候选者。它比在SQL中尝试它更简单,也可能更可靠和更高效。

SQLsharp T-SQL CLR extension library的免费版本中有INET_URIEncodeINET_URIDecode个函数。它也处理Unicode,但您需要付费版本来处理非标准%uXXYY编码。

答案 2 :(得分:1)

来自SQL Sunday的Daniel Hutmacher提供了一个很好的功能 https://sqlsunday.com/2013/04/07/url-encoding-function/

Expression 'f0_' is not present in the GROUP BY list

答案 3 :(得分:1)

这个问题专门要求一个函数,但是,如果您无法创建任何函数,则可以使用以下方法对url进行编码:

select replace27
from TableName
cross apply (select replace1 = replace(T.TagText, '%', '%25')) r1
cross apply (select replace2 = replace(replace1, '&', '%26')) r2
cross apply (select replace3 = replace(replace2, '$', '%24')) r3
cross apply (select replace4 = replace(replace3, '+', '%2B')) r4
cross apply (select replace5 = replace(replace4, ',', '%2C')) r5
cross apply (select replace6 = replace(replace5, ':', '%3A')) r6
cross apply (select replace7 = replace(replace6, ';', '%3B')) r7
cross apply (select replace8 = replace(replace7, '=', '%3D')) r8
cross apply (select replace9 = replace(replace8, '?', '%3F')) r9
cross apply (select replace10 = replace(replace9, '@', '%40')) r10
cross apply (select replace11 = replace(replace10, '#', '%23')) r11
cross apply (select replace12 = replace(replace11, '<', '%3C')) r12
cross apply (select replace13 = replace(replace12, '>', '%3E')) r13
cross apply (select replace14 = replace(replace13, '[', '%5B')) r14
cross apply (select replace15 = replace(replace14, ']', '%5D')) r15
cross apply (select replace16 = replace(replace15, '{', '%7B')) r16
cross apply (select replace17 = replace(replace16, '}', '%7D')) r17
cross apply (select replace18 = replace(replace17, '|', '%7C')) r18
cross apply (select replace19 = replace(replace18, '^', '%5E')) r19
cross apply (select replace20 = replace(replace19, ' ', '%20')) r20
cross apply (select replace21 = replace(replace20, '~', '%7E')) r21
cross apply (select replace22 = replace(replace21, '`', '%60')) r22
cross apply (select replace23 = replace(replace22, '*', '%2A')) r23
cross apply (select replace24 = replace(replace23, '(', '%28')) r24
cross apply (select replace25 = replace(replace24, ')', '%29')) r25
cross apply (select replace26 = replace(replace25, '/', '%2F')) r26
cross apply (select replace27 = replace(replace26, '\', '%5C')) r27

此解决方案的局限性在于它不能替代ASCII控制字符或非ASCII字符。

请注意,第一个替换是%,这一点很重要,因此我们不会转义任何转义码。

答案 4 :(得分:-9)

要使用此脚本,您需要使用Numbers表。

CREATE FUNCTION [dbo].[URLEncode] 
    (@decodedString VARCHAR(4000))
RETURNS VARCHAR(4000)
AS
BEGIN
/******
*       select dbo.URLEncode('K8%/fwO3L mEQ*.}')
**/

DECLARE @encodedString VARCHAR(4000)

IF @decodedString LIKE '%[^a-zA-Z0-9*-.!_]%' ESCAPE '!'
BEGIN
    SELECT @encodedString = REPLACE(
                                    COALESCE(@encodedString, @decodedString),
                                    SUBSTRING(@decodedString,num,1),
                                    '%' + SUBSTRING(master.dbo.fn_varbintohexstr(CONVERT(VARBINARY(1),ASCII(SUBSTRING(@decodedString,num,1)))),3,3))
    FROM dbo.numbers 
    WHERE num BETWEEN 1 AND LEN(@decodedString) AND SUBSTRING(@decodedString,num,1) like '[^a-zA-Z0-9*-.!_]' ESCAPE '!'
END
ELSE
BEGIN
    SELECT @encodedString = @decodedString 
END

RETURN @encodedString

END
GO

该脚本在SQL Server Central上完全可用(需要注册)