2016-01-07 72 views
0

如何构建一个查询,该查询将从SQL Server表中返回仅包含一个或n个“0”(零)的记录。SQL查询返回记录匹配0或00或000或...

600002340234 ==> should not be returned 
0000000 ==> should be returned 
0 ==> should be returned 
00 ==> should be returned 
93800e3 ==> should not be returned 
9103300020 ==> should not be returned 

在我做类似如下的时刻,但这并不理想

select * from table 
where field like '0' or field like '00' .... 

回答

4

所以,它应该有非零长度,它应该包含任何的一个0

select * from table 
where LEN(field) > 0 and field not like '%[^0]%' 
+0

这是为我工作,谢谢! – Michael

1

你想记录其中:

  • 与零

  • 开始不包含任何这不是一个零。

像这样:

where field like '0%' and not field like '%[^0]%'