7

How can I url decode a value in Oracle?

I have a URL encoded string stored in oracle DB table. I want to url_encode it while selecting the results. Any quick way to achieve this ?

Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
TopCoder
  • 4,206
  • 19
  • 52
  • 64

2 Answers2

6

Oracle provides utl_url package containing two functions escape() and unescape() which allow you encode and decode urls. To decode an encoded url string http://www.%24-%26-%3C-%3E-%3F, for example, we can do the following:

SQL> select utl_url.unescape('http://www.%24-%26-%3C-%3E-%3F') as res
  2   from dual
  3  ;

Result:

RES
---------------------
http://www.$-&-<->-?

Note. If you need to use escape() function, you wont be able to use it in a select statement directly, because the second parameter of the function is of Boolean datatype. You will need to write a wrapper function.

SQL> create or replace function url_encode(p_url in varchar2)
  2  return varchar2
  3  is
  4  begin
  5    return utl_url.escape(p_url, true);
  6  end;
  7  /
Function created

SQL> 
SQL> select Url_encode('http://www.$-&-<->-?') as res
  2   from dual
  3  ;

Result:

RES
-------------------------------------
http%3A%2F%2Fwww.%24-%26-%3C-%3E-%3F
Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
  • doesn't decode unicode like Thai or Benghazi `select utl_url. unescape('%E0%B8%95%E0%B8%A5%E0%B8%B2%E0%B8%94%E0%B9%80%E0%B8%A5%E0%B9%87%E0%B8%81-%E0%B8%AD%E0%B8%B4%E0%B8%AA%E0%B8%B2%E0%B8%99%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%AD%E0%B8%87') as res from dual;` gives `à¸à¸¥à¸²à¸à¹à¸¥à¹à¸-อิสาà¸à¸à¸±à¸à¹à¸­à¸` – Toolkit Apr 10 '19 at 16:30
  • `select utl_url. unescape('%E0%B8%95%E0%B8%A5%E0%B8%B2%E0%B8%94%E0%B9%80%E0%B8%A5%E0%B9%87%E0%B8%81-%E0%B8%AD%E0%B8%B4%E0%B8%AA%E0%B8%B2%E0%B8%99%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%AD%E0%B8%87', 'UTF-8') as res from dual` did the trick – Toolkit Apr 10 '19 at 16:34
2

This worked for me:

utl_url.unescape(replace('your text here', '+', ' '),'UTF-8')

Oranit Dar
  • 1,539
  • 18
  • 17