Parse Domain from a given URL in T-SQL
- by Adam N
I fount this answer, but wanted to expand on the question and couldn't find any solutions here on stack or through searching google.
Substring domainname from URL SQL
Basically the link above solves my problem with a simple URL like parsing "www.google.com" with the result of google.
What I am looking for to expand on that is the solution from the link above doesn't help with url's like 'www.maps.google.com' that just returns maps.
WHat I would like is to have it return 'google' from the url 'www.maps.google.com' or return 'example' from 'www.test.example.com'.
If anyone has a solution to this, I would greatly appreciate it.
Update: To be more specific I will also need parsing on second level domains etc. 'www.maps.google.com.au' to return 'google'
Here is my Sql function.
CREATE FUNCTION [dbo].[parseURL] (@strURL varchar(1000))
RETURNS varchar(1000)
AS
BEGIN
IF CHARINDEX('.', REPLACE(@strURL, 'www.','')) > 0
SELECT @strURL = LEFT(REPLACE(@strURL, 'www.',''), CHARINDEX('.',REPLACE(@strURL, 'www.',''))-1)
Else
SELECT @strURL = REPLACE(@strURL, 'www.','')
RETURN @strURL
END