URL SQL 中的子字符串域名

发布于 2024-10-30 19:02:04 字数 333 浏览 1 评论 0原文

我有一组数据:

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

有些数据有 www.,有些没有。有些有 .com/.com.sg/.com.ul,有些则没有。

如何仅提取名称,例如 googleyahoomarketwatchbingbbc 使用 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.sg/.com.ul, some doesn't.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尐籹人 2024-11-06 19:02:04

使用 CHARINDEX 和 SUBSTRING 的 MS SQL Server 语法,您可以执行类似的操作...

(故意过度拆分以使每个步骤变得明显。)

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

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
生活了然无味 2024-11-06 19:02:04

您可以使用 SQL 中的 Replace 函数删除 www . 如果不存在,它将保留字符串原样。

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

编辑

抱歉,我错过了结尾 - 根据您提供的示例数据,这将提取名称:

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

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
沉溺在你眼里的海 2024-11-06 19:02:04
;with cte as
(
  select replace(URL, 'www.', '')+'.' as url
  from myTable
)
select
  left(url, charindex('.', url)-1)
from cte
;with cte as
(
  select replace(URL, 'www.', '')+'.' as url
  from myTable
)
select
  left(url, charindex('.', url)-1)
from cte
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文