Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-28
3
Medium Priority
?
359 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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