create function dbo.splitFuhao(@CID nvarchar(2000), @delimiter nchar) returns varchar(60) as begin declare @idx as int declare @count as int declare @tempCID as varchar(50) declare @tempt as varchar(50) declare @t as varchar(50) set @count = 1 set @t = '' while len(@CID) > 0 begin set @idx = charindex(@delimiter, @CID) if @idx = 0 and len(@CID) > 0 begin -- insert into @t values(@CID) break end if @idx > 1 begin if(left(@CID, @idx - 1)='true') begin if(@count>1) begin set @t = @t+',' end set @t = @t+convert(varchar(10),@count) end set @CID = right(@CID, len(@CID) - @idx) end else set @CID = right(@CID, len(@CID) - @idx) set @count=@count+1 end return @t end go
print dbo.splitFuhao('true-true-false-true-false-true-false', '-')
返回1,2,4,6