日期:2014-05-16  浏览次数:20867 次

这样的order by 应当怎样写
某table有一varchar字段mark_code,数据格式如下#1,#2,#3,……,#10,……,请问怎样写sql的order   by   使select的结果如“#1,#2,#3,……,#10,……”排列?

------解决方案--------------------
ORDER BY SUBSTRING(mark_code,2)+0
------解决方案--------------------
order by replace(mark_code, '# ',1)
------解决方案--------------------
order by cast(substring(name,2) as unsigned) asc
------解决方案--------------------
declare @myTable table(a varchar(10))
insert into @myTable
select '#1 ' union all
select '#2 ' union all
select '#3 ' union all
select '#4 ' union all
select '#5 ' union all
select '#6 ' union all
select '#7 ' union all
select '#9 ' union all
select '#10 ' union all
select '#11 '

select * from @myTable order by convert(int,substring(a,2,len(a)))