select * from mytble where substring(myname,1,4)='ABCD' and convert(int,substring(myname5,4))<1000

解决方案 »

  1.   

    如果存在一条ABCDE999的数据,那么执行convert(int,substring(myname,5,4))<1000,这时等于把E999转换成数字,应该会出错吧,这个有办法解决吗,是否通过进一步的筛选?
      

  2.   

    union all select * from mytable 
    drop table mytablecreate table mytable(MyId int, MyName varchar(10))
    Insert into mytable 
    select '1','ABCD123456'
    union all select '2','ABCD12345'
    union all select '3','ABCD1234'
    union all select '4','ABCD123'
    union all select '5','ABCD12'
    union all select '6','ABCD1'
    union all select '7','ZZZZ123456'
    union all select '8','ZZZZ12345'
    union all select '9','ZZZZ1234'
    union all select '10','ZZZZ123'select * from mytable where substring(myname,1,4)='ABCD' and convert(int,substring(myname,5,4))<1000
    結果:
    4 ABCD123
    5 ABCD12
    6 ABCD1
      

  3.   

    select * from mytble where substring(myname,1,4)='ABCD' and substring(myname,5,1)in([0-9]) and convert(int,substring(myname5,4))<1000
      

  4.   

    select * from mytable where substring(myname,1,4)='ABCD' and substring(myname,5,1)like '[0-9]' and convert(int,substring(myname,5,4))<1000