Friday, February 24, 2012

Domain name from URL

Does anyone know how to extract just the domain from a URL in T-SQL? So,
for example, http://www.awebsite.com/pages/thispage.html" would come out as
http://www.awebsite.com, or just www.awebsite.com.
Many thanks for any help.SELECT
substring(REPLACE('http://www.awebsite.com/pages/thispage.html','http://',''
),0,CHARINDEX('/',REPLACE('http://www.awebsite.com/pages/thispage.html','htt
p://','')))
HTH. Ryan
"Chris Pratt" <not@.given.com> wrote in message
news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
> Does anyone know how to extract just the domain from a URL in T-SQL? So,
> for example, http://www.awebsite.com/pages/thispage.html" would come out
> as http://www.awebsite.com, or just www.awebsite.com.
> Many thanks for any help.
>|||Something like this?
declare @.string varchar(1024)
declare @.UriScheme varchar(16)
set @.string = 'http://www.awebsite.com/pages/thispage.html'
select @.UriScheme = substring(@.string, 0, patindex('%://%', @.string))
set @.string = substring(@.string, patindex('%://%', @.string) + 3, len(@.string
))
select @.UriScheme + '://' + substring(@.string, 0, charindex('/', @.string))
ML
http://milambda.blogspot.com/|||Chris
DECLARE @.fullurl VARCHAR(1000)
SET @.fullurl = 'http://www.cnn.com/articles/sports/show.asp?id=4'
SELECT SUBSTRING(@.fullurl, CHARINDEX('//', @.fullurl)+2,
CHARINDEX('/', SUBSTRING( @.fullurl,
CHARINDEX('//', @.fullurl)+2, LEN(@.fullurl)))-1 )
"Chris Pratt" <not@.given.com> wrote in message
news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
> Does anyone know how to extract just the domain from a URL in T-SQL? So,
> for example, http://www.awebsite.com/pages/thispage.html" would come out
> as http://www.awebsite.com, or just www.awebsite.com.
> Many thanks for any help.
>|||That worked brilliantly, thanks.
"Ryan" <Ryan_Waight@.nospam.hotmail.com> wrote in message
news:eeh19dBIGHA.2928@.TK2MSFTNGP10.phx.gbl...
> SELECT
> substring(REPLACE('http://www.awebsite.com/pages/thispage.html','http://',
''),0,CHARINDEX('/',REPLACE('http://www.awebsite.com/pages/thispage.html','h
ttp://','')))
> --
> HTH. Ryan
> "Chris Pratt" <not@.given.com> wrote in message
> news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
>|||That works great (see above posting!), except for two possible scenarios.
The first is where the URL is actually just the domain name anyway - for
example http://www.awebsite.com. You can get round this by forcing a
trailing '/' to the URL string your are testing, so that one is ok.
The other is if the site begins "https" instead of "http", in which case
just "https:" is returned. Would it be possible to cater for this as well?
Many thanks again,
Chris
"Ryan" <Ryan_Waight@.nospam.hotmail.com> wrote in message
news:eeh19dBIGHA.2928@.TK2MSFTNGP10.phx.gbl...
> SELECT
> substring(REPLACE('http://www.awebsite.com/pages/thispage.html','http://',
''),0,CHARINDEX('/',REPLACE('http://www.awebsite.com/pages/thispage.html','h
ttp://','')))
> --
> HTH. Ryan
> "Chris Pratt" <not@.given.com> wrote in message
> news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
>

No comments:

Post a Comment