有表如下:表明 :Goods 
  编号    名称
  GS001   A
  ...     ....
  GS999   N
  GS1000  B当执行下列语句时:Select Max(编号) from Goods 时 每次查询出来的都是GS999的记录如何查询出GS1000这条记录,也是说不管是GS后面的数字是多少,必须取得数字最大的一个值,而不是按照字符排列的最大的值,如何解决!

解决方案 »

  1.   

    select *
    from goods
    where 编号=(
      select 'GS'+max(cast(substring(编号,3,10) as int))
      from goods
    )
      

  2.   

    select * from goods where 编号=(select max(编号) from goods)这样应该也可以
      

  3.   


    CREATE TABLE [tttt] (
    [Col001] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL, 
    [Col002] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL
    ) ON [PRIMARY]
    GOinsert into tttt
    select 'GS001','1' union all
    select 'GS999','2' union all
    select 'GS1000','3' union all
    select 'HY998','4' union all
    select 'HY999','5' union all
    select 'HY1000','6' union all
    select 'BT1001','7'  select * from tttt
    where col001 in (
    select max(substring(col001,1,2))+
    convert(varchar,max(convert(int,substring(col001,3,len(col001) -2)))) 
    from tttt 
    group by substring(col001,1,2)
    )if exists (select * from dbo.sysobjects where id = object_id(N'[tttt]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [tttt]
    GO-------------
    result:GS1000 3
    HY1000 6
    BT1001 7