Substring domainname from URL SQL

你说的曾经没有我的故事 提交于 2019-12-02 04:33:49

问题


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.sg/.com.ul, some doesn't.

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


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/5578379/substring-domainname-from-url-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!