日期:2014-05-18  浏览次数:20560 次

如何得到一排整数当中最后的那个整数
如何得到一排整数当中最后的那个整数,或者是从一个字符串中得到最后一个子字符串(子字符串用,分开)
格式(1,2,321,234,456)
此时我想得到456

------解决方案--------------------
declare @a varchar(100)
set @a= '1,2,321,234,456 '
select reverse(left(reverse(@a),charindex( ', ',reverse(@a))-1))
/*
--------------------------
456

(所影响的行数为 1 行)
*/
------解决方案--------------------
declare @s varchar(20)
set @s= '1,2,321,234,456 '

select right(@s,charindex( ', ',reverse(@s)+ ', ')-1)

------解决方案--------------------
select reverse(left(reverse( '1,2,321,234,456 '),charindex( ', ',reverse( '1,2,321,234,456 '))-1))
------解决方案--------------------
create function f_splitStr(@s varchar(8000),@split varchar(10))
returns @tb table(col varchar(100))
as
begin
declare @t table(id int identity,b bit)
insert @t select top 500 0 from syscolumns a,syscolumns b

insert @tb select substring(@s,id,charindex(@split,@s+@split,id)-id)
from @t
where id <=len(@s+ '! ') and charindex(@split,@split+@s,id)=id

return
end
go

select top 1 col from dbo.f_splitStr( '1,2,321,234,456 ', ', ') order by col desc
select top 1 col from dbo.f_splitStr( 'a,cc,afefe,aef,qq ', ', ') order by col desc
drop function f_splitStr

456

qq
------解决方案--------------------
declare @s as varchar(20)
set @s = '1,2,321,234,456 '

select reverse(left(reverse(@s),charindex( ', ',reverse(@s)) - 1)) as result

result
--------------------
456

(所影响的行数为 1 行)