SQL语句,如何将一个字段中的一部分字符串作为条件查询

比如有一个字段 “Code”,里面存储了这么几个值:

我需要查询出后四位数字为0的记录:10000、20000、30000,对应的SQL语句该怎么写呀?
我要的是精确查找,不要模糊查找。求答案!!!

使用SQL的substr函数即可。

    该方式格式如下:

    substr( string, start_position, [ length ] );
    string:源字符串
    start_position:提取的位置,字符串中第一个位置始终为1;
    [ length ]:提取的字符数,如果省略,substr将返回整个字符串;

    函数功能:截取函数,可以实现提取字符串中指定的字符数;

    针对本例举例说明:

    select * from 表名 where substr([D],1,2)=“10”

    语句功能说明:从指定表中查询D字段第1、2个字符为“10的记录”。

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-10-13
 select *
from 表
where substring(code,2,1)=0 and substring(code,3,1)=0 and substring(code,4,1)=0 and  substring(code,5,1)=0

 or

select *

from 表

where substring(code,2,4)='0000'

本回答被提问者采纳
第2个回答  2015-03-03
select * from xx where substring(code,2,4)='0000'
select * from xx where substring(code,2,4) like '0000'
substring()函数,第一参数是条件字段,第二参数是截取字符开始位数,第三字段是截取长度
第3个回答  2017-10-28
select * from table where length(column) = 某个值
length()是计算字符串长度的函数,不同的数据库,可能不一样。
第4个回答  2017-11-14
select * from table  where code%10000 = 0

直接求模取余,能整除10000的就是尾数就是4个0

相似回答