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

急!!请教如果排序
varchar(100)的一个数据列,含数据 'B01-09-03 ', '10003-02-01 ', '1-1-3 ', 'A02-02-01 ', 'A02-02-02 ', '1-1-4 '

得到一个顺序结果
'1-1-3 '
'1-1-4 '
'10003-02-01 '
'A02-02-01 '
'A02-02-02 '
'B01-09-03 '


------解决方案--------------------
CREATE function udf_splitstring
(
@str varchar(8000) --要分拆的字符串
,@spli varchar(10) --字符串分隔符
)
returns @retab table(istr varchar(8000))
as
begin
declare @i int
declare @splen int
select @splen=len(@spli),@i=charindex(@spli,@str)
while @i > 0
begin
insert into @retab
values(left(@str,@i-1))
select @str=substring(@str,@i+@splen,8000)
select @i=charindex(@spli,@str)
end
if @str <> ' ' insert into @retab values(@str)
return
end
GO

select istr
from udf_splitstring( ' ' 'B01-09-03 ' ', ' '10003-02-01 ' ', ' '1-1-3 ' ', ' 'A02-02-01 ' ', ' 'A02-02-02 ' ', ' '1-1-4 ' ' ', ', ')
order by left(istr,charindex( '- ',istr)-1)
------解决方案--------------------
set nocount on
declare @a table(a varchar(100))
insert @a select 'B01-09-03 '
insert @a select '10003-02-01 '
insert @a select '1-1-3 '
insert @a select 'A02-02-01 '
insert @a select 'A02-02-02 '
insert @a select '1-1-4 '

select * from @a order by case when isnumeric(left(a,1)) =1 then charindex( '- ',a) else 20 end,a