如下: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  为什么?

解决方案 »

  1.   

    select CAST( replace(Code,'zj-','') as int) from 表A
      

  2.   

    select max(CAST( replace(Code,'zj-','') as int)) from 表A
      

  3.   

    -- 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( replace(Code,'zj-','') as int)) from ta
    --Result:
    /*
    ----------- 
    10000(所影响的行数为 1 行)*/
    --End 
      

  4.   

    -- 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 'zj-4523' 
    Go
    --Start
    select max(CAST( replace(Code,'zj-','') as int)) from ta
    --Result:
    /*
    ----------- 
    10000(所影响的行数为 1 行)*/
    --End 
      

  5.   

    select max(CAST( replace(Code,'zj-','')Code 前面不一定是zj-
    code数据
    zj-10000
    hz-10000
    wz-
    ......
      

  6.   

    SQL codeselect CAST(isnull(max(right(Code,5)),0) as int) from 表A数据: 
    Code 
    zj-10000 
    9999 结果:9999  
    为什么会是9999 ,而不是10000? 可否指明原因?
      

  7.   

    -----------------------max(right(Code,5)) -- 这是字符的比较,9 > 1 
      

  8.   

    这样??????????????
    -- 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 
      

  9.   

    结果是10000啊select CAST(isnull(max(right('zj-10000',5)),0) as int)
    select right('zj-10000',5),max(10000),isnull(10000,0),cast(10000 as int)你的是2000吗
      

  10.   

    select CAST(isnull(max(right('zj-4523',5)),0) as int)
    应该没有异议吧,结果确定
      

  11.   

    TO happyflystone为什么你的会是10000?而我的:
    SQL codeselect CAST(isnull(max(right(Code,5)),0) as int) from 表A
    数据: 
    Code 
    zj-10000 
    9999 结果:9999  
    为什么会是9999 ,而不是10000? 
      

  12.   


    在以下数据: 
    Code 
    zj-10000 
    9999 结果:9999  
      

  13.   

    TO happyflystone If object_id('ta') is not null 
        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
      

  14.   

    max 和cast换一下位置呀select  max(cast(right(Code,5) as int))  from ta 
      

  15.   

    这样会出现14楼的情况To happyflystone
    我再加分
      

  16.   

    select  isnull(max(cast(right(Code,5) as int)),0)  from ta 
      

  17.   


    select  isnull(max(cast(right(Code,5) as int)),0)  from ta 
      

  18.   

    这样也行select  max(cast(right(isnull(Code,0),5) as int))   from ta 
      

  19.   

    select max(cast(right(code,len(code)-charindex('-',code)) as int)) from ss
      

  20.   

    --> --> (Andy)生成测试数据 2008-06-11
    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*/