风过的影子

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
拆分一个用逗号分隔的字符串,(指定位置)
/*
select dbo.GetCheckTab('SortValue1,SortValue3,SortValue8,SortValue9,SortValue10,',5)
*/
create function GetCheckTab
(
    
@TbNames    nvarchar(500),    --查询字符串(逗号分隔)
    @Tbi        int        --用于监测类别循环(用于@TbNames的循环)
)
returns nvarchar(100)
as
begin
    
declare @NowCheckTab nvarchar(200)    --返回表名值
    declare @DealTbNames nvarchar(200)    --处理后的字符串
    declare @Start int        --开始截取位置
    declare @end int        --结束截取位置
    declare @i    int            --循环次数
    set @i = 0
    
set @DealTbNames = @TbNames
    
while @i<@Tbi-1
    
begin
        
set @Start = charindex(',',@DealTbNames)+1
        
set @DealTbNames = substring(@DealTbNames,@Start,len(@DealTbNames))
        
set @i = @i+1
    
end
    
set @end = charindex(',',@DealTbNames)
    
set @NowCheckTab = substring(@DealTbNames,0,@end)
    
return @NowCheckTab
end
执行结果:sortvalue10
posted on 2009-07-31 16:39  风过的影子  阅读(5478)  评论(0编辑  收藏  举报