简体   繁体   English

SQL查询:如何将varchar转换为SQL Server 2000的有符号整数?

[英]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. 我尝试“按演员排序(面板没有签名)”,它适用于mysql,但不适用于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? 您可以调整如何使用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. 请注意,列名panel no包含一个空白,因此必须根据T-SQL的规则对其进行引用。

See this demo . 看到这个演示
This solution is compatible with SQL Server 2000. 该解决方案与SQL Server 2000兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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