Home >Database >Mysql Tutorial >Oracle 模糊查询中不包括某字符串的实现方式

Oracle 模糊查询中不包括某字符串的实现方式

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:24:411653browse

有这么一个需求,查询号码以11结尾,并且从第四位到第八位数字中不能包含ldquo;4rdquo;,最开始以为oracle 和sql server一样支

有这么一个需求,查询号码以11结尾,并且从第四位到第八位数字中不能包含“4”,最开始以为Oracle 和sql server一样支持 [^4],上网查了很多也是说支持的,我反正用了没效果。 万般无奈下,用了instr  和substr 两个函数 才得以解决。

instr( string1, string2 [, start_position [, nth_appearance ] ] )  :取得字符串在指定字符串中的索引

  参数分析:

  string1

  源字符串,要在此字符串中查找。

  string2

  要在string1中查找的字符串.

  start_position

代表string1 的哪个位置开始查找。此参数可选,如果省略默认为1. 字符串索引从1开始。如果此参数为正,从左到右开始检索,,如果此参数为负,从右到左检索,返回要查找的字符串在源字符串中的开始索引。

  nth_appearance

  代表要查找第几次出现的string2. 此参数可选,如果省略,默认为 1.如果为负数系统会报错。

查找的时候如果没有找到返回 0,找到了就会返回找到的字符的在查找的字符串索引。

substr( string, start_position [, length ] ):取得字符串中指定起始位置和长度的字符串

  参数分析:

  string

  源字符串,要在此字符串中截取。

  start_position

  要在string中截取的开始位置。

  length

  要在string中从开始位置截取到指定的长度结束。

语句其实很简单:

select count(*) from tb_name where no like '%11' and instr(substr(no, 4,5),'4')0

这样子就可以了。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn