4

这些是我运行的查询的结果:

10
10.5
11
11.5
12
12.5
13
5
5.5
6
6.5
7
7.5
8
9.5
8.5
9

列的数据类型是nvarchar.

如何按结果排序?

我试过了Order by ascdesc但什么也没做。

4

3 回答 3

7

用于CAST将值视为数字:

ORDER BY CAST(columnName AS FLOAT)
于 2013-07-03T15:24:44.677 回答
3

Alphabetical sorting is different than numeric sorting. Cast your column to a float or real.

select cast(your_column as float) float_col
from your_table
order by float_col;

Ideally, though, you should either

  • store floating-point values in a column of type float or real, or
  • use an exact numeric type that supports decimals (type numeric or type decimal).

It's not clear from your question whether your application is better off with a float or with an exact numeric.

于 2013-07-03T15:31:20.243 回答
2

使用 varchar 你不能......因为它会按字母顺序排列......

那是11会来的7。甚至101会来之前77

最好float作为数据类型。

于 2013-07-03T15:26:31.100 回答