TOP_LEVEL_DOMAIN
Descriptionβ
The TOP_LEVEL_DOMAIN function is used to extract the top-level domain from a URL. If the input URL is invalid, it returns an empty string.
Syntaxβ
TOP_LEVEL_DOMAIN(<url>)
Parametersβ
Parameter | Description |
---|---|
<url> | The URL string from which to extract the top-level domain. Type: VARCHAR |
Return Valueβ
Returns VARCHAR type, representing the extracted top-level domain.
Special cases:
- Returns NULL if url is NULL
- Returns an empty string if url is not a valid URL format
- For multi-level domains (e.g., .com.cn), returns the last level domain
Examplesβ
- Basic domain processing
SELECT top_level_domain('www.baidu.com');
+-----------------------------------+
| top_level_domain('www.baidu.com') |
+-----------------------------------+
| com |
+-----------------------------------+
- Multi-level domain processing
SELECT top_level_domain('www.google.com.cn');
+---------------------------------------+
| top_level_domain('www.google.com.cn') |
+---------------------------------------+
| cn |
+---------------------------------------+
- Invalid URL processing
SELECT top_level_domain('wwwwwwww');
+------------------------------+
| top_level_domain('wwwwwwww') |
+------------------------------+
| |
+------------------------------+