如下:select CAST(isnull(max(right(Code,5)),0) as int) from 表A数据:
Code
zj-10000
9999结果:9999
为什么会是9999 ,而不是10000?或数据:
Code
zj-10000
zj-4523结果:-4523 为什么?
Code
zj-10000
9999结果:9999
为什么会是9999 ,而不是10000?或数据:
Code
zj-10000
zj-4523结果:-4523 为什么?
If object_id('ta') is not null
Drop table ta
Go
Create table ta(Code varchar(8))
Go
Insert into ta
select 'zj-10000' union all
select '9999'
Go
--Start
select max(CAST( replace(Code,'zj-','') as int)) from ta
--Result:
/*
-----------
10000(所影响的行数为 1 行)*/
--End
If object_id('ta') is not null
Drop table ta
Go
Create table ta(Code varchar(8))
Go
Insert into ta
select 'zj-10000' union all
select 'zj-4523'
Go
--Start
select max(CAST( replace(Code,'zj-','') as int)) from ta
--Result:
/*
-----------
10000(所影响的行数为 1 行)*/
--End
code数据
zj-10000
hz-10000
wz-
......
Code
zj-10000
9999 结果:9999
为什么会是9999 ,而不是10000? 可否指明原因?
-- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(Code varchar(8))
Go
Insert into ta
select 'zj-10000' union all
select '9999'
Go
--Start
select max(cast(right(Code,5) as int)) from ta --Result:
/*
-----------
10000(所影响的行数为 1 行)*/
--End
select right('zj-10000',5),max(10000),isnull(10000,0),cast(10000 as int)你的是2000吗
应该没有异议吧,结果确定
SQL codeselect CAST(isnull(max(right(Code,5)),0) as int) from 表A
数据:
Code
zj-10000
9999 结果:9999
为什么会是9999 ,而不是10000?
在以下数据:
Code
zj-10000
9999 结果:9999
Drop table ta
Go
Create table ta(Code varchar(8))
Go
Insert into ta
select null
Go
--Start
select max(cast(right(Code,5) as int)) from ta 结果:
null
在这种情况下,我希望结果是0
所以我才用select CAST(isnull(max(right(Code,5)),0) as int) from 表A
我再加分
select isnull(max(cast(right(Code,5) as int)),0) from ta
Set Nocount On
declare @1 table([Code] nvarchar(8))
Insert @1
select N'zj-10000' union all
select N'9999'
select Max(Convert(int,right(Code,5))) From @1
/*
先要转换成int,再Max(),不然按字符比较大小,返回自然是9999了
-----------
10000*/