应该用到这个吧,但具体就等高手了charindex()

解决方案 »

  1.   

    --示例
    declare @url varchar(8000)
    set @url='http://www.xici.net:80/xiaoqi/2.htm'--处理
    declare @t table(项目 varchar(20),值 varchar(100))
    declare @i int
    set @i=charindex('://',@url)-1
    insert @t select '协议号',left(@url,@i)select @url=stuff(@url,1,@i+3,'')
    ,@i=charindex(':',@url)
    if @i=0
    begin
    set @i=charindex('/',@url)-1
    insert @t select '域名或IP地址',left(@url,@i)
    union all select '端口号',''
    union all select '文件路径',stuff(@url,1,@i+1,'')
    end
    else
    begin
    insert @t select '域名或IP地址',left(@url,@i-1)
    select @url=stuff(@url,1,@i,''),@i=charindex('/',@url)-1
    insert @t select '端口号',left(@url,@i)
    union all select '文件路径',stuff(@url,1,@i+1,'')
    end--显示结果:
    select * from @t
    /*--测试结果项目                   值                      
    -------------------- --------------------------
    协议号                  http
    域名或IP地址            www.xici.net
    端口号                  80
    文件路径                xiaoqi/2.htm(所影响的行数为 4 行)--*/
      

  2.   

    如URL中没有端口号,如:http://www.xici.net/xiaoqi/2.htm则要求代码同样能给出默认端口号80,这个应该不难,只是提醒注意一下,谢谢:0
      

  3.   

    格式应该是固定的吧,若是则如下:declare @s varchar(8000)
    set @s='http://www.xici.net:80/xiaoqi/2.htm'
    select 协议号=left(@s,charindex(':',@s)-1)
    set @s=right(@s,len(@s)-charindex(':',@s)-1)
    while left(@s,1)='/'
      set @s=right(@s,len(@s)-1)
    select 域名或IP地址=left(@s,charindex(':',@s)-1)
    set @s=right(@s,len(@s)-charindex(':',@s))
    select 端口号=left(@s,charindex('/',@s)-1)
    set @s=right(@s,len(@s)-charindex('/',@s))
    while left(@s,1)='/'
      set @s=right(@s,len(@s)-1)
    select 文件路径=@s
      

  4.   

    --需求又变了:
    declare @s varchar(8000)
    set @s='http://www.xici.net:80/xiaoqi/2.htm'select 协议号=left(@s,charindex(':',@s)-1)
    set @s=right(@s,len(@s)-charindex(':',@s)-1)
    while left(@s,1)='/'
      set @s=right(@s,len(@s)-1)
    if charindex(':',@s)>0
    begin
      select 域名或IP地址=left(@s,charindex(':',@s)-1)
      set @s=right(@s,len(@s)-charindex(':',@s))
      select 端口号=left(@s,charindex('/',@s)-1)
    end
    else
    begin
      select 域名或IP地址=left(@s,charindex('/',@s)-1)
      select 端口号='80'
    end
    set @s=right(@s,len(@s)-charindex('/',@s))
    while left(@s,1)='/'
      set @s=right(@s,len(@s)-1)
    select 文件路径=@s
      

  5.   

    TO pbsql(风云):你这样似乎端口号的问题未解决,端口号有时是没有的:(
      

  6.   

    --写成自定义函数
    create function f_spliturl(
    @url varchar(8000)
    )returns @re table(项目 varchar(20),值 varchar(100))
    as
    begin
    --处理
    declare @i int
    set @i=charindex('://',@url)-1
    if @i=-1
    insert @re select '协议号','http' --没有协议号默认为:http
    else
    begin
    insert @re select '协议号',left(@url,@i)
    select @url=stuff(@url,1,@i+3,'')
    end set @i=charindex(':',@url)
    if @i=0
    begin
    set @i=charindex('/',@url)-1
    insert @re select '域名或IP地址',left(@url,@i)
    union all select '端口号','80' --没有端号号默认为:80
    union all select '文件路径',stuff(@url,1,@i+1,'')
    end
    else
    begin
    insert @re select '域名或IP地址',left(@url,@i-1)
    select @url=stuff(@url,1,@i,''),@i=charindex('/',@url)-1
    insert @re select '端口号',left(@url,@i)
    union all select '文件路径',stuff(@url,1,@i+1,'')
    end
    return
    end
    go--调用
    select * from f_spliturl('http://www.xici.net:80/xiaoqi/2.htm')
    select * from f_spliturl('ftp://www.xici.net/xiaoqi/2.htm')
    select * from f_spliturl('www.xici.net:80/xiaoqi/2.htm')
    go--删除测试
    drop function f_spliturl/*--测试结果项目                   值                
    -------------------- --------------------
    协议号                  http
    域名或IP地址            www.xici.net
    端口号                  80
    文件路径                 xiaoqi/2.htm(所影响的行数为 4 行)项目                   值                
    -------------------- --------------------
    协议号                  ftp
    域名或IP地址            www.xici.net
    端口号                  80
    文件路径                 xiaoqi/2.htm(所影响的行数为 4 行)项目                   值                
    -------------------- -------------------
    协议号                  http
    域名或IP地址            www.xici.net
    端口号                  80
    文件路径                 xiaoqi/2.htm(所影响的行数为 4 行)
    --*/
      

  7.   

    declare @s varchar(100),@http varchar(50),@dns varchar(100),@port varchar(20),@file varchar(100)
    set @s='http://www.xici.net:80/xiaoqi/2.htm'
    set @http=left(@s,charindex(':',@s)-1)
    set @dns=left(replace(@s,@http+'://',''),charindex('/',replace(@s,@http+'://',''))-1)
    if charindex(':',@dns)<0
    begin
          set @port='无'        
    end
    else
    begin
          set @port=right(@dns,len(@dns)-charindex(':',@dns))
          set @dns=left(@dns,charindex(':',@dns)-1)   
    end
    set @file=right(replace(@s,@http+'://',''),len(replace(@s,@http+'://',''))-charindex('/',replace(@s,@http+'://','')))
    print @http
    print @dns
    print @port
    print @file
      

  8.   

    多谢各位,尤其是邹建大师。另有一简单问题,刚才发了,但不见显示,所以在这里一并求教:一个存储过程需要选则一批数据,同时要将选中的数据的某个字段的值统一修改成另一个值,比如:表:userbaseID Username Type1  zhangsan 0
    2  lisi     1
    3  wangwu   0
    4  haha     2
    5  titi     2
    ......想选该表中 type为0的字段的所有数据并将这些数据的Type值改为2,怎么写?
      

  9.   

    select * into #t from userbase where type=0
    update userbase set type=2 where type=0
    select * from #t
      

  10.   

    这个问题弱智了点,不过别笑我啊!没学好T-SQL,呵呵:0
      

  11.   

    不好意思,判断的地方改一下,应该是
    if charindex(':',@dns)<0    --改成=0,我以为找不到返回-1
      

  12.   

    邹建大师,可能我没讲清楚问题:表:userbaseID Username Type1  zhangsan 0
    2  lisi     1
    3  wangwu   0
    4  haha     2
    5  titi     2
    6  wangda   0
    ......我的意思是,比如按ID顺序选5条记录,然后将选出的5条记录中的ID=1 和 ID=3的两个记录的Type字段的值改为2,而ID=6的记录的Type字段值保持不变,且在原表中操作。不知道我这样表达是否清楚:0
      

  13.   

    --不太明白(是这个意思吗?)--按id顺序选出5条记录
    select top 5 id into #t from userbase order by id--更新 userbase 中的 Type
    update a set Type=2
    from userbase a,#t b
    where a.id=b.id and a.Type<>2
      

  14.   

    考虑默认问题就复杂了,比如www.microsoft.com    --协议号应该是http  至于路径就没法确定了
    microsoft.com        --协议号应该是http 域名应该是www.microsoft.com 路径就没法确定还有如果传入asp或者xml的url,比如这个贴的url:
    http://community.csdn.net/Expert/topic/3308/3308994.xml?temp=.825268
    协议号:http 
    域名:community.csdn.net
    端口号:80       --默认80到底对不对???
    文件路径:Expert/topic/3308/3308994.xml?temp=.825268 --不对吧 ,temp=.825268应该是参数所以这个问题应该有格式限制,才能写出合适的语句!
      

  15.   

    --写在一个语句中
    update a set Type=2
    from userbase a,(select top 5 id from userbase order by id)b
    where a.id=b.id and a.Type<>2
      

  16.   

    zjcxc(邹建) :
      a.Type<>2??    应该是  a.Type=0吧  楼主根本没说清楚
      
      

  17.   

    sorry:再说一次,我的问题是这样的:表:userbaseID Username Type1  zhangsan 0
    2  lisi     1
    3  wangwu   0
    4  haha     2
    5  titi     2
    6  wangda   0
    ......我的意思是,比如按ID顺序选前5条记录(要求Type=0),然后将选出的5条记录中的ID=1 和 ID=3的两个记录的Type字段的值改为2,而ID=6的记录的Type字段值应该保持不变,且在原表中操作。就是说,执行完语句后,表应该变成:ID Username Type1  zhangsan 2
    2  lisi     1
    3  wangwu   2
    4  haha     2
    5  titi     2
    6  wangda   0    (不变,仍为零,因为不是被选中的5条记录中的,所以Type不变)
    ......真是不好意思,麻烦各位了!尤其是大师:)