2011-07-02 29 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