2011-07-02 32 views
2

我怎样写SQL语句来安排此列使用SQL安排与数字,而不是字母列

columnA 
------- 
10A 
11C 
12V 
10D 
8F 
8R 
9C 

,使其返回顺序

columnA 
------- 
8F 
8R 
9C 
10A 
10D 
11C 
12V 

一个结果(以数字然后按字母顺序)?

我已经试过这样一句话:

Select columnA from tblStudents order by columnA 

,但并未奏效。

+0

SQL-Server? MySQL的? Postgres的?甲骨文?这些功能是系统相关的。 –

+0

凯文,接受你的问题的答案,不要离开他们打开 –

回答

4

您将不得不将字符串拆分为数字部分和文本部分,并将数字部分转换为实际数字。

我不知道你正在使用哪种SQL方言;这是针对Microsoft SQL Server:

select columnA 
from tblStudents 
order by 
    convert(int, substring(columnA, 1, patindex('%[^0-9]%') - 1)), 
    substring(columnA, patindex('%[^0-9]%'), 1000) 

patindex('%[^0-9]%')会找到字符串中的第一个非数字的,所以第一个substring得到字符,直到那个点,第二substring得到字符从该点字符串的结尾。

+0

非常感谢你我会试试这个 – Kevin