Solved

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

Posted on 2004-09-28
3
351 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
3 Comments
 
LVL 2

Accepted Solution

by:
gbajramo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now