1.通过创建一个函数来处理
create function [dbo].[F_RemoveStartOrEndString] ( @result varchar(2000),--要处理的字符串 @Separate varchar(10),--分隔符 @type int--0:删除头 1:刪除尾 2:头尾都刪除 ) returns varchar(2000) as begin declare @oldType int set @oldType=@type set @result=isnull(@result,'') set @Separate=isnull(@Separate,'') if @Separate='' or @result='' return @result labSkipleft: if left(@result,len(@Separate))=@Separate and @type=0 set @result=substring(@result,len(@Separate)+1,len(@result)) if right(@result,len(@Separate))=@Separate and @type=1 set @result=substring(@result,1,len(@result)-len(@Separate)) set @type=(case when @oldType<>2 then -1 else @type-1 end) if @type>=0 goto labSkipleft return @result end---实例如下----------------------
select dbo.F_RemoveStartOrEndString(',10,12,13,16,11,',',',0)结果为:10,12,13,16,11,
select dbo.F_RemoveStartOrEndString(',10,12,13,16,11,',',',1)结果为:,10,12,13,16,11
select dbo.F_RemoveStartOrEndString(',10,12,13,16,11,',',',2)结果为:10,12,13,16,11