SQL 如何查找某个字符串第N次出现的位置

if exists(select 1 from sysobjects where name='char_index')
drop function char_index

create function char_index(@string varchar(8000),@char varchar(10),@index smallint)
--@string:待查找字符串,@index:查找位置
returns smallint
as
begin
 
declare
 
@i tinyint,--当前找到第@i个
  @position tinyint--所在位置
  set @position=1;
 
set @i=0;
 
while charindex(@char,@string,@position)>0
 
begin
   
set @position=charindex(@char,@string,@position)+1;
   
set @i=@i+1;
   
if @i=@index
   
begin
    
return @position-1;
   
end
 
end
 
return 0;--0表示未找到
end

select dbo.char_index('sdf_dsf_dfgdg_ertr_erte','f_',2)

http://blog.csdn.net/zhou968/article/details/2760871

转载于:https://www.cnblogs.com/Hdsome/archive/2011/12/29/2306523.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值