日期:2014-05-19  浏览次数:20413 次

高手看过来,如何取一行数据中的部分数据
例如   id       aa
          1         1234555555   343434   243431  
          2         大幅度飞飞飞飞   23321   23231
          3         23232     222   221   221  
我的取值规则是从aa列中取,从空格后面是2的开始取,直到取到1为止,上面的数据取出的结果应该为
24343
2332   2323
222   22   22

通过SQL查询,哪位高手有法?

------解决方案--------------------
create table #t(id int, aa varchar(100))
insert into #t
select 1, '1234555555 343434 243431 ' union all
select 2, '大幅度飞飞飞飞 23321 23231 ' union all
select 3, '23232 222 221 221 '

select * from #t

select left(bb,charindex( '1 ',bb)-1)
from (
select right(aa,len(aa)-charindex( ' 2 ',aa)) as bb
from #t) tt

drop table #t


/*
--楼主的结果应该是这样吧

24343
2332
222 22

*/
------解决方案--------------------
create table lz(id int,aa varchar(100))
insert into lz values(1, '1234555555 343434 243431 ')
insert into lz values(2, '大幅度飞飞飞飞 23321 23231 ')
insert into lz values(3, '23232 222 221 221 ')


select
replace(
substring(left(aa,len(aa)-charindex( '1 ',REVERSE(aa))),charindex( ' 2 ',aa)+1,len(aa)),
'1 ', ' ')
from lz