Created
April 15, 2021 11:17
-
-
Save m1roff/d32a1e7155f0d66e7efc8d5e5ff07adf to your computer and use it in GitHub Desktop.
Parse Top Level Domain from URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS getTopLevelDomain; | |
DELIMITER ## | |
CREATE FUNCTION getTopLevelDomain (strURL varchar(1000), level tinyint) | |
RETURNS varchar(1000) | |
BEGIN | |
IF ISNULL(level) THEN | |
SET level = 2; | |
END IF; | |
IF POSITION('http://' IN strURL) > 0 OR POSITION('https://' IN strURL) > 0 | |
THEN | |
-- Remove not needed stuff | |
SET strURL = REPLACE(strURL,'https://',''); | |
SET strURL = REPLACE(strURL,'http://',''); | |
SET strURL = REPLACE(strURL,'www.',''); | |
-- Remove everything after “/” if one exists | |
IF POSITION('/' IN strURL) > 0 THEN | |
SET strURL = LEFT(strURL,POSITION('/' IN strURL)-1); | |
END IF; | |
IF LENGTH(strURL) - LENGTH(REPLACE(strURL,'.','')) > (level - 1) then | |
set strURL = SUBSTRING_INDEX(strURL, '.', (level * -1)); | |
end if; | |
return strURL; | |
END IF; | |
return NULL; | |
END ## | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment