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
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分:0)
如果您的产品编号格式是明确定义的(即它始终是'J12M'后跟一个数字),那么您可以删除'固定'部分并为您的排序转换数字部分。有点像:
ORDER BY CONVERT(int, REPLACE(productnumber, 'J12M', ''))
答案 3 :(得分:0)
您可以这样订购: -
select product_number
from vtfe_product
order by CONVERT(int, substring(product_number, 5, len(product_number) ) );
答案 4 :(得分: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
样本