Solved

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

Posted on 2004-09-28
3
356 Views
Last Modified: 2012-06-27
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
Stevod
0
Comment
Question by:Stevod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
gbajramo earned 500 total points
ID: 12167913
I would use a fuction as so:

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

    BEGIN
        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)
            BEGIN
                SET @ResStr     =     @ResStr + @TmpStr
                SET @Cnt     =     @Cnt + 1
                SET @TmpStr     =     substring(@Url,@Cnt,1)
                SET @TmpAsc     =     isnull(AscII(@TmpStr),0)
            END
        RETURN @ResStr
END

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.
0
 
LVL 4

Expert Comment

by:Excalibur_Software
ID: 12169405
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
0
 
LVL 12

Expert Comment

by:kselvia
ID: 12172785
Or:

Select

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.)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question