首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

查寻某字符串在第N次出现的位置

2012-08-16 
查找某字符串在第N次出现的位置USE [CobraDGServerLog]GO/****** 对象:UserDefinedFunction [dbo].[char_i

查找某字符串在第N次出现的位置

USE [CobraDGServerLog]GO/****** 对象:  UserDefinedFunction [dbo].[char_index]    脚本日期: 05/08/2012 16:33:59 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO  create function [dbo].[char_index](@string varchar(8000),@char varchar(10),@index smallint)  --@string:待查找字符串,@index:查找位置    returns smallint   as    begin     declare     @i int,--当前找到第@i个    @position int--所在位置    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   

?

热点排行