--如果只查询0开头,并且由0组成的:select * from 表 where replace(字段,'0','')='' and 字段 charindex('0',字段)=1

解决方案 »

  1.   

    考虑好了再回答,0开头,而且是任意个0组成的字符串!
    说明一点,我试过这个方法:
    if convert(int,@str)=0
    如果@str正好是任意个0组成的字符串那是没问题的,但是如果@str像0abc  abc这样的串那convert函数会首先出错,所以仍然不能满足要求!
      

  2.   

    zjcxc(邹建) 的办法不错,不过也有漏洞,如果字符串的中间有空格的话,比如‘00  0’,也能查询出来,应再加一个条件,如下:select * from 表 
        where replace(字段,'0','')='' and 
              字段 charindex('0',字段)=1 and
              charindex('1',replace(字段,' ','1'))<>0
      

  3.   

    错了错了应是:charindex('1',replace(字段,' ','1'))=0
    select * from 表 
        where replace(字段,'0','')='' and 
              字段 charindex('0',字段)=1 and
              charindex('1',replace(字段,' ','1'))=0
      

  4.   

    sorry!楼上的正确,我把我误把' '写成''了。
      

  5.   

    --这样没漏洞了吧?select * from 表 where datalength(replace(字段,'0','')='')=0 and 字段 charindex('0',字段)=1
      

  6.   

    如果有key 
    select a.* from tablename a join (select key,col from tablename where isnumeric(col)>0) b on a.key=b.key and cast(b.col as int)=0
      

  7.   

    zjcxc(邹建),你真聪明,我怎么没想到啊,学习!