在SQL中执行升序

时间:2015-09-05 12:19:27

标签: sql sql-order-by

如何按升序获取此数据?

输出=

 GRN
 ------------------
 GRNV-1415-RESH-132
 GRNV-1415-RESH-13
 GRNV-1417-RESH-2
 GRNV-1415-RESH-8
 GRNV-1416-RESH-12
 GRNV-1411-RESH-1
 GRNV-1415-RESH-13
 GRNV-1415-RESH-99

我的查询是:

select GRN 
from tbl_receipt 
where Active = 1
  and ArticleContrasted = 1 
  and LocationID = 41  
order by 
   left(GRN, patindex('%[a-zA-Z0-9]%', GRN)-1) asc

1 个答案:

答案 0 :(得分:0)

看起来您正在使用SQL Server。

您最终可以使用以下方式获取号码:

select right(grn, charindex('-', reverse(grn)) - 1)

然后,将其转换为数字,所以:

select left(grn, len(grn) - charindex('0', reverse(grn))),
       cast(right(grn, charindex('-', reverse(grn)) - 1) as int)