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

order 排序问题
有一个字段是这样的格式850×495×475,我想按第一个数字(比如850,有时候是3位,有时候是4位)排序.没法用left(guige,3)来排序.

请问怎么办?

我不太清楚sql语句对字符串的操作还有哪些函数可以用的.


------解决方案--------------------
use lairui

select * from sky order by left(lairui,(len(lairui)-charindex( '* ',lairui))) desc
------解决方案--------------------
if object_id( 't1 ') is not null
drop table t1

create table t1(id int identity(1,1),guige varchar(20))
insert into t1 select '850×495×475 '
union all select '50×495×475 '
union all select '1850×495×475 '
union all select '2850×495×475 '
union all select '3850×495×475 '
union all select '80×495×475 '
union all select '8500×495×475 '

select * from t1 order by cast(left(guige,charindex( '× ',guige)-1) as int)

drop table t1 --

(所影响的行数为 7 行)

id guige
----------- --------------------
2 50×495×475
6 80×495×475
1 850×495×475
3 1850×495×475
4 2850×495×475
5 3850×495×475
7 8500×495×475

(所影响的行数为 7 行)