按 varchar 的数字顺序
Numeric order by varchar
大家好,我正在尝试对我的 varchar 列进行排序。我几乎完成了它,但我找不到解决以下问题的方法:
-999, -998, 0, 8A, 80, 80A, 90, A80, ADBA
目前是(见代码):
-998, -999, 0, 8A, 80, 80A, 90, A80, ADBA
我只需要修复负值。有人找到解决这个问题的办法了吗?
SELECT RANK_NUMBER FROM SHIPMENT_EVENT
ORDER BY CASE WHEN ISNUMERIC(RANK_NUMBER) = 1 THEN RIGHT('0000000000' + RANK_NUMBER, 10) ELSE CASE
WHEN LEFT(RANK_NUMBER, 1) LIKE '[0-9]' THEN RIGHT(
'0000000000'
+ LEFT(SUBSTRING(RANK_NUMBER, PATINDEX('%[0-9.-]%', RANK_NUMBER
), 80
),
PATINDEX
('%[^0-9.-]%', SUBSTRING(RANK_NUMBER, PATINDEX('%[0-9.-]%',
RANK_NUMBER),
80
) + 'X') -1), 10)
ELSE RANK_NUMBER
END
END
我希望是这样的:
order by cast(left(col, patindex('%[^-0-9]%', col + 'x') - 1) as decimal(20, 0))
大家好,我正在尝试对我的 varchar 列进行排序。我几乎完成了它,但我找不到解决以下问题的方法:
-999, -998, 0, 8A, 80, 80A, 90, A80, ADBA
目前是(见代码): -998, -999, 0, 8A, 80, 80A, 90, A80, ADBA
我只需要修复负值。有人找到解决这个问题的办法了吗?
SELECT RANK_NUMBER FROM SHIPMENT_EVENT
ORDER BY CASE WHEN ISNUMERIC(RANK_NUMBER) = 1 THEN RIGHT('0000000000' + RANK_NUMBER, 10) ELSE CASE
WHEN LEFT(RANK_NUMBER, 1) LIKE '[0-9]' THEN RIGHT(
'0000000000'
+ LEFT(SUBSTRING(RANK_NUMBER, PATINDEX('%[0-9.-]%', RANK_NUMBER
), 80
),
PATINDEX
('%[^0-9.-]%', SUBSTRING(RANK_NUMBER, PATINDEX('%[0-9.-]%',
RANK_NUMBER),
80
) + 'X') -1), 10)
ELSE RANK_NUMBER
END
END
我希望是这样的:
order by cast(left(col, patindex('%[^-0-9]%', col + 'x') - 1) as decimal(20, 0))