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

给字符串字段排序。。
我有个商品表product。里面有个价格字段。因为价格可以为“面议”也可以为数字。
所以将价格设为字符串类型的了,现在排序如果光是以order   by排序的话,结果是不对的。。。
求正确的排序方法?
我要的结果是如果价格分别为1。11。111。22。222。33。面议时
升序结果为:1,11,22,33,111,222,面议。
而不是:1。11。111。22。222。33。面议(这是字符串排序的结果)

------解决方案--------------------
declare @product table (price varchar(100))
insert @product
select '1 '
union all select '11 '
union all select '111 '
union all select '22 '
union all select '222 '
union all select '33 '
union all select '面议 '

select * from @product order by case when isnull(price, 0) = '面议 ' then 9999999999 else cast(price as numeric(10,2)) end
/*
price
1
11
22
33
111
222
面议
*/

select * from @product order by case when isnull(price, 0) = '面议 ' then 9999999999 else cast(price as numeric(10,2)) end desc
/*
面议
222
111
33
22
11
1
*/

------解决方案--------------------
---创建测试环境
Declare @product Table(价格 Varchar(8))
Insert @product Select '1.23 '
Union All Select '面议 '
Union All Select '11 '
Union All Select '111 '
Union All Select '22.23 '
Union All Select '2.22 '
Union All Select '33 '
Union All Select '面议 '


---查询结果
select 价格 from @product order by case 价格 when '面议 ' then 99999999 else cast(价格 as numeric(10,2)) end