2015-09-05 40 views
0

如何获取此数据的升序?在SQL中以升序排列

输出=

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 
+2

您正在使用什么RDBMS? – Mureinik

回答

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)