Extracts a component from a URL string based on the specified part value.
Syntax
string parse_url(string <url>, string <part>[, string <key>])Parameters
url
Required. STRING. The URL to parse. Returns an error if the URL is invalid or malformed.
part
Required. STRING. The URL component to extract. Not case-sensitive.
The following table maps each part value to the corresponding URL component:
part value | URL component | Description |
|---|---|---|
HOST | example.com | The domain name or IP address |
PATH | /over/there/index.dtb | The path to the resource |
QUERY | type=animal&name=narwhal | The value that corresponds to key when key is specified |
REF | nose | The fragment identifier (the part after #) |
PROTOCOL | file | The scheme (for example, http, https, file) |
AUTHORITY | username:password@example.com:8042 | The authority section: user info, host, and port |
FILE | — | The file component |
USERINFO | username:password | The user credentials before the @ symbol |
key
Optional. STRING. When part is QUERY, returns the value for this specific query parameter key.
Return value
Returns a STRING value. Returns NULL if url, part, or key is NULL. Returns an error if part is not one of the valid values listed above.
Examples
All examples use the same URL:
file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose-- Returns example.com
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'HOST');
-- Returns /over/there/index.dtb
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'PATH');
-- Returns animal (the value of the query parameter "type")
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'QUERY', 'type');
-- Returns nose
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'REF');
-- Returns file
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'PROTOCOL');
-- Returns username:password@example.com:8042
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'AUTHORITY');
-- Returns username:password
SELECT parse_url('file://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose', 'USERINFO');Related functions
parse_url is a string function. For more information about related string functions, see String functions.