1
 J12M1
 J12M14
 J12M15
 J12M18
 J12M2
 J12M21
 J12M22
 J12M23
 J12M24
 J12M25
 J12M26
 J12M27
 J12M28
 J12M29
 J12M35
 J12M36
 J12M39

但我的需要是

 J12M1
 J12M2
 J12M3

我可以在查询中做什么

select productnumber from vt_fe_product order by productnumber
4

5 回答 5

2

您可以在前面加上零:

SELECT 
   productnumber 
FROM   
   vt_fe_product 
ORDER BY 
   Right( '0000000000' + productnumber, 10 ) 

(来自:https ://stackoverflow.com/a/3326371/284240 )

演示

结果:

PRODUCTNUMBER
J12M1
J12M2
J12M14
J12M15
J12M18
J12M21
J12M22
J12M23
J12M24
J12M25
J12M26
J12M27
J12M28
J12M29
J12M35
J12M36
J12M39
于 2013-02-08T11:08:33.297 回答
1

也许您需要尝试使用额外的 LEN 功能?

SELECT ProductName
FROM
(
 SELECT 
 'J12M1' AS ProductName
 UNION ALL
 SELECT 
 'J12M14' AS ProductName
 .......................
 UNION ALL
 SELECT 
 'J12M24' AS ProductName
 UNION ALL
 SELECT 
 'J12M25' AS ProductName
 UNION ALL
 SELECT 
 'J12M35' AS ProductName
 UNION ALL
 SELECT 
 'J12M36' AS ProductName
 UNION ALL
 SELECT 
 'J12M39' AS ProductName
) AS Products
ORDER BY LEN(ProductName), ProductName
于 2013-02-08T11:14:26.413 回答
0

您可以这样订购:-

select product_number from vtfe_product order by CONVERT(int, substring(product_number, 5, len(product_number) ) );

于 2013-02-08T11:24:58.903 回答
0

已经看到其他答案了。决定走不同的路线。有点冗长,但这个解决方案的好处是你最终会按实际数字排序,它会首先按“J”数字排序。

SELECT 
    -- Get the first numeric component
    CAST (
        SUBSTRING(
            productnumber, 
            CHARINDEX('J', productnumber, 1) + 1, 
            CHARINDEX('M', productnumber, 1) - CHARINDEX('J',productnumber, 1) - 1)
        AS INT 
    ) AS FirstSort,
    -- Get the second numeric component
    CAST (
        SUBSTRING(
            productnumber, CHARINDEX('M', productnumber, 1) + 1, LEN(productnumber))
        AS INT
    ) AS SecondSort,
    productnumber
FROM
    productnumbers
ORDER BY
    FirstSort,
    SecondSort

演示

http://sqlfiddle.com/#!6/fd371/2

于 2013-02-08T11:27:18.343 回答
0

如果您的产品编号格式定义明确(即它始终是“J12M”后跟一个数字),那么您可以删除“固定”部分并为您的排序转换数字部分。类似于以下内容:

ORDER BY CONVERT(int, REPLACE(productnumber, 'J12M', ''))
于 2013-02-08T11:10:21.113 回答