简体   繁体   中英

SQL query: how to convert varchar to signed integer for SQL Server 2000?

I got a column called panel no

panel no
--------
1
10
1A
2A
6
...

I would like to have the order like

panel no
--------
1
1A
2A
6
10

I try "order by cast(panel no as signed)" and it works for mysql but not in SQL Server 2000.

what can I do?

You could adapt the solution of How to get the numeric part from a string using T-SQL? to your needs:

SELECT 
    * 
FROM 
    TableA
WHERE
    columnA = 'abc'
ORDER BY
    CAST(LEFT([panel no], patindex('%[^0-9]%', [panel no] + '.') - 1) AS INT);

Please note that the column name panel no contains a blank, so it's got to be quoted according the rules of T-SQL.

See this demo .
This solution is compatible with SQL Server 2000.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM