sorry for my english
i have this query to extract domain from urls
SELECT SUBSTRING(LEFT(url, LOCATE(\'/\', url, 8) - 1), 8) AS domain...
I tried multiple examples on this page (and some documentation) docs to make the following, annotated, version. It appears impossible with SUBSTRING_INDEX to remove subdomains without removing *.co.uk type domains.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(url,
'?', 1), # split on url params to remove weirdest stuff first
'://', -1), # remove protocal http:// https:// ftp:// ...
'/', 1), # split on path
':', 2), # split on user:pass
'@', 1), # split on user:port@
':', 1), # split on port
'www.', -1), # remove www.
'.', 4), # keep TLD + domain name
'/', 1)
AS domain
FROM (
SELECT 'http://test.com' as url
UNION SELECT 'https://test.com'
UNION SELECT 'http://test.com/one'
UNION SELECT 'http://test.com/?huh'
UNION SELECT 'http://www.test1.test.com?http://ouch.foo'
UNION SELECT 'test.com'
UNION SELECT 'test.com/one'
UNION SELECT 'test.com/one/two'
UNION SELECT 'test.com/one/two/three'
UNION SELECT 'test.com/one/two/three?u=http://maaaaannn'
UNION SELECT 'http://one.test.com'
UNION SELECT 'one.test.com/one'
UNION SELECT 'https://www.bbc.co.uk/'
UNION SELECT 'http://a.very.complex-domain.co.uk:8080/foo/bar'
UNION SELECT 'postgres://user:pass@host.com:5432/path?k=v#f'
UNION SELECT 'http://10.64.3.5/data_check/index.php?r=index/rawdatacheck'
UNION SELECT 'two.one.test.com/one' ) AS test;