我有一组数据:
www.google.com.sg www.yahoo.com marketwatch bing.com bbc.co.uk
有些数据有www.
,有些数据没有。有些人有.com
/ .com.sg
/ .com.ul
,有些则没有。
如何仅提取名称,例如使用SQL的google
,yahoo
,marketwatch
,bing
,bbc
?
答案 0 :(得分:2)
使用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
答案 1 :(得分:1)
您可以使用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
答案 2 :(得分:0)
;with cte as
(
select replace(URL, 'www.', '')+'.' as url
from myTable
)
select
left(url, charindex('.', url)-1)
from cte