首先从表数字中排序数据,然后是不带asc或desc的字符串

时间:2018-04-18 05:29:43

标签: sql sql-server tsql

我有一个表格,其列值为

45
55
47
AB
56
AC
43

我想排序为

45
55
47
56
43
AB
AC

首先表示数字,然后表示字符串,不按asc或desc排序

怎么做请建议我

3 个答案:

答案 0 :(得分:2)

SQL 中,你可以在patindex()子句中使用order by函数

order by patindex('%[a-z]%', col), ?;

但是,?指定表数据排序的实际col

答案 1 :(得分:1)

试试这个......

               SELECT '45'  AS Val
               INTO Temp
               UNION ALL SELECT '55'
               UNION ALL SELECT '155'
               UNION ALL SELECT '47'
               UNION ALL SELECT 'AB'
               UNION ALL SELECT '56'
               UNION ALL SELECT 'AC'
               UNION ALL SELECT '43'
               UNION ALL SELECT 'DSE'


               SELECT *
               FROM Temp
               ORDER BY Val

答案 2 :(得分:0)

您还可以尝试以下查询

DECLARE @Table TABLE(col1 VARCHAR(10))
INSERT INTO @Table VALUES(45)
INSERT INTO @Table VALUES(55)
INSERT INTO @Table VALUES(47)
INSERT INTO @Table VALUES('AB')
INSERT INTO @Table VALUES(56)
INSERT INTO @Table VALUES('AC')
INSERT INTO @Table VALUES(43)

SELECT Col1
FROM @Table
WHERE ISNUMERIC(col1) = 1
UNION
SELECT * FROM @Table WHERE ISNUMERIC(col1) = 0 ORDER BY Col1 ASC