-- Collection of code snippets by Arne Vajhøj 
-- posted to eksperten.dk, usenet and other places (2002-now) 
DROP FUNCTION find_domain
GO

CREATE FUNCTION find_domain(@url VARCHAR(255))
RETURNS VARCHAR(255)
AS
BEGIN
    DECLARE @temp VARCHAR(255)
    SET @temp = LOWER(RTRIM(LTRIM(@url)))
    SET @temp = REPLACE(@temp,'http://','')
    SET @temp = REPLACE(@temp,'https://','')
    SET @temp = REPLACE(@temp,'www.','')
    IF CHARINDEX('/',@temp) > 0
    BEGIN
        SET @temp = LEFT(@temp,CHARINDEX('/',@temp)-1)
    END
    RETURN @temp
END
GO
