URL functions
AnalyticDB for MySQL provides URL functions for parsing, extracting, and encoding URL strings in SQL queries.
| Function | Description |
|---|---|
| PARSE_URL | Extracts a named component from a URL. |
| URL_DECODE | Decodes an application/x-www-form-urlencoded string. |
| URL_ENCODE | Encodes a string using the application/x-www-form-urlencoded format. |
| URL_EXTRACT_FRAGMENT | Extracts the fragment identifier from a URL. |
| URL_EXTRACT_HOST | Extracts the hostname or IP address from a URL. |
| URL_EXTRACT_PARAMETER | Extracts the value of a named query parameter from a URL. |
| URL_EXTRACT_PATH | Extracts the path from a URL. |
| URL_EXTRACT_PORT | Extracts the port number from a URL. |
| URL_EXTRACT_PROTOCOL | Extracts the protocol from a URL. |
| URL_EXTRACT_QUERY | Extracts the query string from a URL. |
PARSE_URL
PARSE_URL(url, part [, key]) → varcharExtracts the component specified by part from url. All parameters are of type STRING, and the function returns STRING.
The part value is case-insensitive. Supported values:
part value | Extracted content | Example output |
|---|---|---|
HOST | Hostname or IP address | example.com |
PATH | Path, starting from / | /path/to/resource |
QUERY | Query string, starting from ? | type=animal&name=narwhal |
REF | Fragment identifier, starting from # | nose |
PROTOCOL | Protocol | http, https, file |
AUTHORITY | User info, hostname, and port | username:password@example.com:8042 |
FILE | File path of the URL | — |
USERINFO | Username and password | username:password |
When part is QUERY, pass key to retrieve the value of a specific query parameter.
Returns NULL if url, part, or key is NULL, or if part is an invalid value.
Examples
All examples use the same URL: file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose
-- Extract the host
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'HOST');
-- example.com
-- Extract the path
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'PATH');
-- /over/there/index.dtb
-- Extract the value of the "type" query parameter
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'QUERY', 'type');
-- animal
-- Extract the fragment identifier
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'REF');
-- nose
-- Extract the protocol
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'PROTOCOL');
-- file
-- Extract the authority (user info + hostname + port)
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'AUTHORITY');
-- username:password@example.com:8042
-- Extract the user information
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'USERINFO');
-- username:passwordURL_DECODE
URL_DECODE(input) → varcharDecodes a string encoded in application/x-www-form-urlencoded format. This function is the inverse of URL_ENCODE.
Returns NULL if input is NULL.
Decoding rules
Characters
a–z,A–Z, and0–9are unchanged..,-,*, and_are unchanged.+is converted to a space.Percent-encoded sequences (
%xy) are decoded into their corresponding byte values, then interpreted as a UTF-8 string.
Example
SELECT url_decode('example+for+url_decode%3A%2F%2F+%28fdsf%29');
-- example for url_decode:// (fdsf)URL_ENCODE
URL_ENCODE(input) → varcharEncodes a string using the application/x-www-form-urlencoded format. This function is the inverse of URL_DECODE.
Returns NULL if input is NULL.
Encoding rules
Characters
a–z,A–Z, and0–9are unchanged..,-,*, and_are unchanged.Spaces are converted to
+.All other characters are encoded as UTF-8 bytes in
%xyformat, wherexyis the hexadecimal byte value.
Example
SELECT url_encode('example for url_encode:// (fdsf)');
-- example+for+url_encode%3A%2F%2F+%28fdsf%29URL extraction functions
The following functions extract individual components from a URL. They accept URLs conforming to the standard structure:
[protocol:][//host[:port]][path][?query][#fragment]The extracted value does not include URI syntax separators such as :, ?, or #. All functions accept a STRING argument and return STRING.
All functions return NULL if url is NULL or invalid.
URL_EXTRACT_FRAGMENT
URL_EXTRACT_FRAGMENT(url) → varcharExtracts the fragment identifier (the part after #) from url. Returns an empty string if url contains no fragment identifier.
Example
SELECT url_extract_fragment('http://example.com/path1/p.php?k1=v1&k2=v2#Ref1');
-- Ref1URL_EXTRACT_HOST
URL_EXTRACT_HOST(url) → varcharExtracts the hostname or IP address from url.
Example
SELECT url_extract_host('http://example.com/path1/p.php?k1=v1&k2=v2#Ref1');
-- example.comURL_EXTRACT_PARAMETER
URL_EXTRACT_PARAMETER(url, parameter) → varcharExtracts the value of parameter from the query string of url. Returns NULL if url is NULL, parameter is NULL, or the query string does not contain parameter.
Examples
-- "k1" exists in the query string
SELECT url_extract_parameter('http://example.com/path1/p.php?k1=v1&k2=v2&k3&k4#Ref1', 'k1');
-- v1
-- "k5" does not exist in the query string
SELECT url_extract_parameter('http://example.com/path1/p.php?k1=v1&k2=v2&k3&k4#Ref1', 'k5');
-- NULLURL_EXTRACT_PATH
URL_EXTRACT_PATH(url) → varcharExtracts the path from url. Returns an empty string if url contains no path.
Example
SELECT url_extract_path('http://example.com/path1/p.php?k1=v1&k2=v2#Ref1');
-- /path1/p.phpURL_EXTRACT_PORT
URL_EXTRACT_PORT(url) → varcharExtracts the port number from url. Returns NULL if url contains no port.
Examples
-- No port specified
SELECT url_extract_port('http://example.com/path1/p.php?k1=v1&k2=v2#Ref1');
-- NULL
-- Port is specified
SELECT url_extract_port('http://example.com:8080/path1/p.php?k1=v1&k2=v2#Ref1');
-- 8080URL_EXTRACT_PROTOCOL
URL_EXTRACT_PROTOCOL(url) → varcharExtracts the protocol from url.
Example
SELECT url_extract_protocol('http://example.com/path1/p.php?k1=v1&k2=v2#Ref1');
-- httpURL_EXTRACT_QUERY
URL_EXTRACT_QUERY(url) → varcharExtracts the query string (the part after ?, excluding the ? itself) from url. Returns an empty string if url contains no query string.
Example
SELECT url_extract_query('http://example.com/path1/p.php?k1=v1&k2=v2#Ref1');
-- k1=v1&k2=v2