[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

Parsing a URL in SQL - Any Code Available, Urgently?

I have a database with thousands of rows each of which includes a field containing a single URL such as 'http://www.mysite.com/default/indexpage.htm'. From each row, I want to extract just the website address, so that the above would become 'www.mysite.com'.

I'm working in SQL Server. Does anyone have a SQL User Defined Function, Stored Procedure or similar code which can perform this function?

Many thanks
1 Solution
I would use a fuction as so:

Create FUNCTION StripUrl (@Url varchar(100))
RETURNS varchar(200)

        DECLARE @Cnt AS int,
            @Lng AS int,
            @TmpAsc as int,
            @TmpStr AS varchar(1),
            @ResStr AS varchar(200)
        SET @Lng     =     len(@Url)
      SET @Url = substring(@Url, 8, @Lng)
        SET @ResStr     =     ''
        SET @Cnt     =     1
        SET @TmpStr     =     substring(@Url,@Cnt,1)
        SET @TmpAsc     =     isnull(AscII(@TmpStr),0)

        WHILE (@Cnt <= @Lng) AND (@TmpAsc <> 47) and (@TmpAsc <> 92)
                SET @ResStr     =     @ResStr + @TmpStr
                SET @Cnt     =     @Cnt + 1
                SET @TmpStr     =     substring(@Url,@Cnt,1)
                SET @TmpAsc     =     isnull(AscII(@TmpStr),0)
        RETURN @ResStr

select dbo.StripUrl ('http://www.goran.com\test')

I wrote it so it would strip everything after / or \ which should be helpful. The function assumes you will always have "http://" at the beginning of the string.

You may want to adjust the size of the @url parameter to the size of the field you have in your db.

Hope this helps.
Are you looking just to get the domain name or do you want the network service as well?
What I mean is are you looking for just WebPages or are you looking for other things as well such as ftp site of email addresses, gopher or Archie search service ECT.
here is another function you can use

Public Function GetMyDomainName(Myurl) As String
   Dim StartPos As Integer
   Dim EndPos As Integer
   Dim Tempstring As String

   Tempstring = Trim(Myurl)
   StartPos = InStr(Tempstring, "HTTP://")  'Find the start of you string  

If StartPos > 0 Then
   StartPos = StartPos + 7 ' If you want to change what type of severices you are getting change the start string and the number of
'       character to start string
'       Example if you want ftp:// Then you would add the search string above and change this line to 6

End If

Tempstring = Mid$(Myurl, StartPos, (Len(Myurl) - 2))
EndPos = InStr(Tempstring, "/")

GetMyDomainName = Mid$(Myurl, StartPos, (EndPos) - 1)

End Function
Ken SelviaRetiredCommented:


substring(left(url,charindex('//',url)+1) + left(substring(url + '/',charindex('//',url+'/') + 2,255),charindex('/',substring(url+'/',charindex('//',url+'/') + 2,255)) -1),charindex('/',url)+2,255)

from mytable

That should get any hostname regardless of protocol prefix (ftp, http etc.)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now