日期:2014-05-17  浏览次数:20355 次

去掉数组最后一位
arrid
0,1,177,181
0,1,177,182
0,1,183
0,1,183,184
0,1,183,185

我有一字段arrid,其中数据如下,我想去掉最后那一位,应该怎么写个函数? 不知道有没有Split这样的解决办法,谢谢!   update a set arrid=????  (还有一字段为id,最后一位其实就是id的值,重复了,所以需要去除)


就是想得到下面这样的数据
arrid
0,1,177
0,1,177
0,1
0,1,183
0,1,183

------解决方案--------------------

declare @t table([arrid] varchar(11))
insert @t
select '0,1,177,181' union all
select '0,1,177,182' union all
select '0,1,183' union all
select '0,1,183,184' union all
select '0,1,183,185'

--第一种
select reverse(right(reverse(arrid),len(arrid)-charindex(',',reverse(arrid)))) from @t
--第二种
select left(arrid,len(arrid)-charindex(',',reverse(arrid))) from @t

------解决方案--------------------
--最后一个符号后面的数据长度固定
update tb1 set arrid=substring(arrid,1,LEN(arrid)-4) 
--不固定
update tb1 set arrid=substring(arrid,1,len(arrid)-charindex(',',reverse(arrid)))