TSQL - How to URL Encode

前端 未结 5 1919
清酒与你
清酒与你 2021-02-13 11:22

Looking for a bug free tested sql script that i could use in a UDF to encode a url through sql. Function would take in a URL and pass out a URL Encoded URL. I have seen a few,

5条回答
  •  死守一世寂寞
    2021-02-13 11:37

    The question specifically asks for a function however here is a solution to url encode if your not able to create any functions:

    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
    

    The limitation of this solution it that is does not replace ASCII Control characters or Non-ASCII characters.

    Note it's important that the first replacement is for % so we don't escape any escape codes.

提交回复
热议问题