------------------------------------------------------ -----------------------CRATE BY BGX-------------- --------------------把字符串按某字符分开--------------- -------使用事例 [dbo].[ufnGetsubstring_bgx]('一/二三/四五','/',1,2) 结果 二三 -------使用事例 [dbo].[ufnGetsubstring_bgx]('一/二三/四五','/',0,1) 结果 一 -------使用事例 [dbo].[ufnGetsubstring_bgx]('一/二三/四五','/',0,2) 结果 一/二三 CREATE FUNCTION [dbo].[ufn_Getsubstring_bgx](@instring varchar(256),@subchar varchar(16),@FOstart smallint,@FOend smallint) RETURNS varchar(256) AS BEGIN if @FOstart = @FOend BEGIN RETURN null END else BEGIN if @FOstart > @FOend BEGIN declare @max smallint set @max = @FOstart set @FOstart=@FOend set @FOend END END declare @string varchar(256)--替换查询字符串 declare @Outstring varchar(256)--输出字符串 declare @start smallint--求每字符位置开始处 declare @end smallint--求每字符位置结束处 declare @FIstart smallint--函数输入开始位置 declare @FIend smallint--函数输入结束位置 declare @strlen smallint--查询字符串长度 declare @num1 smallint declare @num2 smallint declare @chartable table (charact char(2),number smallint)--存放每个字符对应位置表 declare @indexnum table (indexnum smallint IDENTITY(0,1) NOT NULL,charact char(2),number smallint);--存放分段字符位置表 set @string=char(128)+REPLACE(@instring,@subchar,CHAR(128))+CHAR(128) select @strlen=len(@string) set @start = 1 while @start <= @strlen begin insert @chartable select SUBSTRING(@string,@start,1),@start set @start= @start+1 end insert @indexnum(charact,number) select * from @chartable where charact = char(128) select @num1 = number+1 from @indexnum where indexnum select @num2 = number+1 from @indexnum where indexnum ; RETURN replace(SUBSTRING(@string,@num1,@num2-@num1-1),CHAR(128),@subchar); END;
浙公网安备 33010602011771号