Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • 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
Stevod
0
Stevod
Asked:
Stevod
1 Solution
 
gbajramoCommented:
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
 
Excalibur_SoftwareCommented:
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
 
Ken SelviaRetiredCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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