环境:VS2008+SQL2005字段:
start_num  nchar(20)  由于字段里要保留0,因此字段类型设置为nchar
 end_num   nchar(20)数据:
start_num   end_num
000351      000400
000401      000450
000451      000500
000501      000550
000551      000600
要求:查询start_num在000401和000501之间的数据,请问如何实现?谢谢

解决方案 »

  1.   

      nchar  好像是 你插入的000551 你设置个数为20个 你插入的数据不够20个 都以“ ”填充把
      

  2.   

    应为该字段代表票据的开头,因此要保留前位0,如果int经过设置可以保留前位0也是可以的。
      

  3.   

    --不知道对不对
    create table #tb(start_num nchar(20),end_num nchar(20))
    insert into #tb 
    select '000351','000400' union all
    select '000401','000450' union all
    select '000451','000500' union all
    select '000501','000550' union all
    select '000551','000600'select * from #tb where cast(start_num as int)>351 and cast(start_num as int)<501
    /*
    start_num end_num
    ---- -----
    000401               000450              
    000451               000500              
    */
      

  4.   

    select * from table where Convert(int,right(start_num,3)) >=401 and Convert(int,right(start_num,3)) <=501
      

  5.   

    要是字符串长度都一样的话,可以直接用下面的语句,不用进行类型转换。select * from table where start_num between '000401' and '000501'
      

  6.   

    用这个转换成功啦,谢谢各位!
    select * from #tb where cast(start_num as int)>351 and cast(start_num as int)<501