Parse Domain from a given URL in T-SQL

Posted by Adam N on Stack Overflow See other posts from Stack Overflow or by Adam N
Published on 2012-12-05T22:52:55Z Indexed on 2012/12/06 23:04 UTC
Read the original article Hit count: 217

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server