开发者

Substring domainname from URL SQL

I have a set of Data:

www.google.com.sg
www.yahoo.com
marketwatch
bing.com
bbc.co.uk

Some data has www., some doesn't. Some has .com/.com.s开发者_Go百科g/.com.ul, some doesn't.

How do I extract just the name e.g. google, yahoo, marketwatch, bing, bbc using SQL?


Using MS SQL Server syntax of CHARINDEX and SUBSTRING you could do something like...

(Deliberately overly split-up to mak eeach step obvious.)

WITH
  url_start AS
(
  SELECT
    *,
    CASE WHEN LEFT(myURL, 4) = 'www.' THEN 4 ELSE 1 END AS d_start
  FROM
    myTable
)
,
  url_end
AS
(
  SELECT
    *,
    CASE WHEN
      CHARINDEX('.', myURL, d_start) = 0
    THEN
      LEN(myURL) + 1
    ELSE
      CHARINDEX('.', myURL, d_start)
    END as d_end
  FROM
    url_start
)
SELECT
  *,
  SUBSTRING(myURL, d_start, d_end - d_start) AS domain
FROM
  url_end


You can use the Replace function in SQL to remove the www. if it doesn't exist it will leave the string as it is.

Select Replace(URLColumn, 'www.','') as [CleanURLColumn]
From YourTable

EDIT

Sorry I missed out the ending - based on the sample data you have provided this will extract the name:

Select  Case
        When CharIndex('.', Replace(URL, 'www.','')) > 0 then
           Left(Replace(URL, 'www.',''), CharIndex('.',Replace(URL, 'www.',''))-1)
        Else
           Replace(URL, 'www.','')
        End as [CleanURL]

From dbo.YourTable


;with cte as
(
  select replace(URL, 'www.', '')+'.' as url
  from myTable
)
select
  left(url, charindex('.', url)-1)
from cte
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜