sql 2005 怎么修改表中字段默认排序规则?

有表a,字段c值为2R,5K,1M。orderbyc后系统默认为:1m,2r,5k正确的排序为:2R,5K,1M换算关系为:1000R=1K,1000K=1M,谢谢。... 有表a , 字段c 值为2R,5K,1M。order by c 后系统默认为:1m,2r,5k 正确的排序为:2R,5K,1M
换算关系为:1000R=1K,1000K=1M ,谢谢。
展开
 我来答
常尔02U
2013-09-24 · TA获得超过132个赞
知道小有建树答主
回答量:220
采纳率:0%
帮助的人:227万
展开全部
select
c
from
a
order by
case when right(c,1)='R' then cast(left(c,len(c)-1) as int)
when right(c,1)='K' then cast(left(c,len(c)-1) as int)*1000
when right(c,1)='M' then cast(left(c,len(c)-1) as int)*1000*1000 end;
追问
非常谢谢你,我问题没有提好,还有一种情况,后面还有数,比如2K40,1K5,20M50。
追答
  select
  c
  from
  a
  order by
  case when charindex('M',c)>0 and charindex('K',c)>0 and len(c)>charindex('K',c) then
  cast(substring(c,1,charindex('M',c)-1) as int)*1000*1000+
  cast(substring(c,charindex('M',c)+1,charindex('K',c)-charindex('M',c)-1 as int)*1000+
  cast(substring(c,charindex('K',c)+1) as int)
  when charindex('M',c)>0 and charindex('K",c)>0 then
  cast(substring(c,1,charindex('M',c)-1) as int)*1000*1000+
  cast(substring(c,charindex('M',c)+1,charindex('K',c)-charindex('M',c)-1 as int)*1000
  when charindex('K',c)>0 and len(c)>charindex('K',c) then
  cast(substring(c,1,charindex('K',c)-1) as int)*1000+
  cast(substring(c,charindex('K',c)+1) as int)
  when charindex('M',c)>0 then
  cast(substring(c,1,charindex('M',c)-1) as int)*1000*1000

  when charindex('K',c)>0 then
  cast(left(c,len(c)-1) as int)*1000
  when charindex('R',c)>0 then
  cast(left(c,len(c)-1) as int)
  end
  
  
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式