select CONVERT(varchar(50),id+1-(select min(id) from 
b where inportdate='20091013' and sp_id='tu')) from a 
现想得出,如果查得结果位数为1则前面补00,位数为2刚补一0,否则取原数要怎样写?

解决方案 »

  1.   

    select RIGHT('000'+CONVERT(varchar(50),id+1-(select min(id) from 
    b where inportdate='20091013' and sp_id='tu')),3)AS C from a 
      

  2.   

    case len('长度') when  = 3 then '结果' 
                     when = 2 then '0' + '结果' 
                     when = 1 then '=0' + '结果' 
    end
         
      

  3.   

    你在哪里补?看不出来.方法是:right('0' + cast(你的.. as varchar),位数)
      

  4.   

    你在哪里补?看不出来. 方法是: right('00' + cast(你的.. as varchar),3)
      

  5.   

    会不会有超过三位的,
    要不然用
    CASE WHEN 来判断
      

  6.   

    with f as
    (
    select 
      CONVERT(varchar(50),id+1-(select min(id) 
    from 
       b 
    where 
       inportdate='20091013' and sp_id='tu'))  as id 
    from 
       a
    )
    select id=case when len(id)=1 then '00'+cast(id as varchar) 
                   when len(id)=2 then '0'+cast(id as varchar)
                   when len(id)=3 then  cast(id as varchar) end
      

  7.   

    超三位用CASE WHEN,楼上有结果了
      

  8.   

    select right(replicate('0',10)+CONVERT(varchar(50),id+1-(select min(id) from 
    b where inportdate='20091013' and sp_id='tu')),3) from a 
      

  9.   

    select right('000'+CONVERT(varchar(50),id+1-(select min(id),3) from 
    b where inportdate='20091013' and sp_id='tu')) from a 
      

  10.   

    修改下
    select right('000'+CONVERT(varchar(50),id+1-(select min(id) from 
    b where inportdate='20091013' and sp_id='tu')),3) from a 
      

  11.   


    select isnull(replicate('0',3-len(s)),'')+ltrim(s)
    from
    (
    select 
    CONVERT(varchar(50),id+1-(select min(id) 
    from b 
    where inportdate='20091013' 
    and sp_id='tu')
    ) s
    from a 
    )t