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
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
您可以在前面加上零:
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
也许您需要尝试使用额外的 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
您可以这样订购:-
select product_number
from vtfe_product
order by CONVERT(int, substring(product_number, 5, len(product_number) ) );
已经看到其他答案了。决定走不同的路线。有点冗长,但这个解决方案的好处是你最终会按实际数字排序,它会首先按“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
演示
如果您的产品编号格式定义明确(即它始终是“J12M”后跟一个数字),那么您可以删除“固定”部分并为您的排序转换数字部分。类似于以下内容:
ORDER BY CONVERT(int, REPLACE(productnumber, 'J12M', ''))