我正在尝试从我的数据库的 url 中提取域名。我尝试的是让我更接近我想要的输出 70%,我无法格式化我表中的几个 url。有人可以建议我处理这种情况的更好方法吗?
website_addr |
---|
abcd |
http://www.google.com |
https://www.yahoo.com |
www.aol.com |
www.ebay.com,www.ebay.com |
https://www.ebix.com/ |
xavier.com |
www.rubix.com |
https://rend-auv.com |
我目前得到的输出:
website_addr | 输出 |
---|---|
abcd | abcd |
http://www.google.com | www.google.com |
https://www.yahoo.com | www.yahoo.com |
www.aol.com | w.aol.com |
www.ebay.com,www.ebay.com | www.ebay.com,www.ebay.com |
www.rubix.com | w.rubix.com |
https://www.ebix.com/ | www.ebix.com |
xavier.com | xavier.com |
https://rend-auv.com | rend-auv.com |
我期望的输出
website_addr | 输出 |
---|---|
abcd | abcd |
http://www.google.com | www.google.com |
https://www.yahoo.com | www.yahoo.com |
www.aol.com | www.aol.com |
www.rubix.com | www.rubix.com |
www.ebay.com,www.ebay.com | www.ebay.com |
https://www.ebix.com/ | www.ebix.com |
xavier.com | xavier.com |
https://rend-auv.com | rend-auv.com |
我尝试了什么。
substring(
substring(add_website_addr,charindex('://',add_website_addr)+3,len(add_website_addr)),
0,
case when charindex('/',substring(website_addr,charindex('://',website_addr)+3,len(website_addr)))>0
then charindex('/',substring(website_addr,charindex('://',website_addr)+3,len(website_addr)))
when charindex('?',substring(website_addr,charindex('://',website_addr)+3,len(website_addr)))>0
then charindex('?',substring(website_addr,charindex('://',website_addr)+3,len(website_addr)))
else
len(website_addr)
end
)
答案 0 :(得分:-1)
您只想删除“http://”或“https://”的前导值。那是准确的吗?有很多方法。一个简单的例子
declare @x table (url varchar(50));
insert @x (url) values
('abcd'), ('http://www.google.com'), ('https://www.yahoo.com'), ('www.aol.com');
select *,
case when left(url, 8) = 'https://' then stuff(url, 1, 8, '')
when left(url, 7) = 'http://' then stuff(url, 1, 7, '')
else url
end as z
from @x;