日期:2014-05-18 浏览次数:20476 次
--> 测试数据:# if object_id('tempdb.dbo.#') is not null drop table # create table #(src varchar(21)) insert into # select 'http://www.abc.com/' union all select 'http://www.a.cn/1.htm' union all select 'http://e.s.d.gov/1/2/' union all select 'http://f.w.d.gov/' union all select 'http://e.c.a.com/' union all select null union all select '空' select *, left( src, charindex('/', src, charindex('//',src)+2) )host from # where src like '%://%' /* src host --------------------- --------------------- http://www.abc.com/ http://www.abc.com/ http://www.a.cn/1.htm http://www.a.cn/ http://e.s.d.gov/1/2/ http://e.s.d.gov/ http://f.w.d.gov/ http://f.w.d.gov/ http://e.c.a.com/ http://e.c.a.com/ */
------解决方案--------------------
declare @url nvarchar(30); declare @index int; set @url='http://baidu.com/hello/world/a.html'; select SUBSTRING(@url,0,(CharIndex('/',replace(@url,'http://',''))+7)) ------------------------------ http://baidu.com (1 行受影响)