+ -
当前位置:首页 → 问答吧 → SQL查询最大值不对。

SQL查询最大值不对。

时间:2011-12-05

来源:互联网

有10列数据。
FILE_NUM
001
002
003
004
005
006
007
008
009
010
FILE_NUM数据类型是nvchar
执行Select MAX(FILE_NUM) as MAX_NUM from table1
结果显示的是 9
为什么显示的不是10呢?
如果要10.那应该怎么写?

作者: cobra_chen   发布时间: 2011-12-05

SQL code

Select MAX(convert(int,FILE_NUM)) as MAX_NUM from table1

作者: geniuswjt   发布时间: 2011-12-05

不会吧!
SQL code
create table tb(FILE_NUM varchar(10))
insert into tb select '001'
insert into tb select '002'
insert into tb select '003'
insert into tb select '004'
insert into tb select '005'
insert into tb select '006'
insert into tb select '007'
insert into tb select '008'
insert into tb select '009'
insert into tb select '010'
go
Select MAX(FILE_NUM) as MAX_NUM from tb
/*
MAX_NUM
----------
010

(1 行受影响)
*/
go
drop table tb

作者: qianjin036a   发布时间: 2011-12-05

Select MAX(cast(FILE_NUM as int)) as MAX_NUM from table1

作者: fredrickhu   发布时间: 2011-12-05

他肯定哪里还有问题,按道理字符排序也应该是010>009的
不过转化为整型肯定没错
引用 2 楼 qianjin036a 的回复:
不会吧!

SQL code
create table tb(FILE_NUM varchar(10))
insert into tb select '001'
insert into tb select '002'
insert into tb select '003'
insert into tb select '004'
insert into tb select '005'……

作者: geniuswjt   发布时间: 2011-12-05

你定义的是varchar类型,max()函数处理的类型应该是数字类型。这里需要先把varchar转换成int,在处理,具体语句如下:
select max(cast (FILE_NUM as int)) as MAX_NUM from table_name

作者: TravyLee   发布时间: 2011-12-05

sql 2000表示无压力
SQL code
Select MAX(FILE_NUM) as MAX_NUM from table1
/*
010
*/

注意结果为为009而不是9,
不知道你的结果是怎么出来的,9

作者: xiaolinyouni   发布时间: 2011-12-05

热门下载

更多