考虑好了再回答,0开头,而且是任意个0组成的字符串! 说明一点,我试过这个方法: if convert(int,@str)=0 如果@str正好是任意个0组成的字符串那是没问题的,但是如果@str像0abc abc这样的串那convert函数会首先出错,所以仍然不能满足要求!
zjcxc(邹建) 的办法不错,不过也有漏洞,如果字符串的中间有空格的话,比如‘00 0’,也能查询出来,应再加一个条件,如下:select * from 表 where replace(字段,'0','')='' and 字段 charindex('0',字段)=1 and charindex('1',replace(字段,' ','1'))<>0
错了错了应是:charindex('1',replace(字段,' ','1'))=0 select * from 表 where replace(字段,'0','')='' and 字段 charindex('0',字段)=1 and charindex('1',replace(字段,' ','1'))=0
sorry!楼上的正确,我把我误把' '写成''了。
--这样没漏洞了吧?select * from 表 where datalength(replace(字段,'0','')='')=0 and 字段 charindex('0',字段)=1
如果有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
说明一点,我试过这个方法:
if convert(int,@str)=0
如果@str正好是任意个0组成的字符串那是没问题的,但是如果@str像0abc abc这样的串那convert函数会首先出错,所以仍然不能满足要求!
where replace(字段,'0','')='' and
字段 charindex('0',字段)=1 and
charindex('1',replace(字段,' ','1'))<>0
select * from 表
where replace(字段,'0','')='' and
字段 charindex('0',字段)=1 and
charindex('1',replace(字段,' ','1'))=0
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