平时我们截取字符串大多数都是在程序里进行,比如用split函数等.由于因为程序的需要和性能,我们更希望选择直接在查询语句里截取.
例如我们现在需要截取电一张表里描述字段里"/"符号以前的字符串,包括"/":
select distinct Substr(字段, 1, Instr(字段, '/', 1)) from 表
如果不包括"/",如下:
select distinct Substr(字段, 1, Instr(字段, '/', 1)-1) from 表
以上假设描述字段里只有一个"/"符号,现在如果有多个"/",我们要取第一个或者第二个"/"符号之前的字符串,不包括"/",如下:
select distinct Substr(字段, 1, Instr(字段, '/', 1,1)-1) from 表
或者
select distinct Substr(字段, 1, Instr(字段, '/', 1,2)-1) from 表