I have a column url encoded with urlencode in php. I wish to make a select like this
SELECT some_mix_of_functions(…) AS Decoded FROM table
Replace is not a good solution because I will have to add all the decoding by hand. Any other solution to get the desire result ?
Yes you can:
This creates a function
decode_url_part, then you can use it like that:Or you can just use the mix of functions and subqueries from the body of the above function.
This doesn’t handle ‘+’ characters (representing whitespace), but I guess adding this is quite easy (if you ever need it).
Also, this assumes utf-8 encoding for non-ascii characters, but you can replace ‘UTF8’ with your own encoding if you want.
It should be noted that the above code relies on undocumented postgresql feature, namely that the results of regexp_matches function are processed in the order they occur in the original string (which is natural, but not specified in docs).
As Pablo Santa Cruz notes, string_agg is a PostgreSQL 9.0 aggregate function. The equivalent code below doesn’t use it (I hope it works for 8.x):