INSTR函数浅析

2024-08-30 22:18
文章标签 函数 浅析 instr

本文主要是介绍INSTR函数浅析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

--INSTR函数


{ INSTR| INSTRB| INSTRC| INSTR2| INSTR4}(string , substring [, position [, occurrence ] ])


The INSTR functions search string for substring. The search operation is defined as comparing the substring argument with substrings of string of the same length for equality until a match is found or there are no more substrings left. Each consecutive compared substring of string begins one character to the right (for forward searches) or one character to the left (for backward searches) from the first character of the previous compared substring. If a substring that is equal to substring is found, then the function returns an integer indicating the position of the first character of this substring. If no such substring is found, then the function returns zero.

返回找到的子串的位置。


position is an nonzero integer indicating the character of string where Oracle Database begins the search—that is, the position of the first character of the first substring to compare with substring. If position is negative, then Oracle counts backward from the end of string and then searches backward from the resulting position.

如果position值为负,则从右往左开始搜寻。


occurrence is an integer indicating which occurrence of substring in string Oracle should search for. The value of occurrence must be positive. If occurrence is greater than 1, then the database does not return on the first match but continues comparing consecutive substrings of string, as described above, until match number occurrence has been found.


Examples

The following example searches the string CORPORATE FLOOR, beginning with the third character, for the string "OR". It returns the position in CORPORATE FLOOR at which the second occurrence of "OR" begins:

SELECT INSTR('CORPORATE FLOOR','OR', 3, 2) "Instring"FROM DUAL;Instring
----------14
--寻找CORPORATE FLOOR中从第三个字符开始第二次出现的'OR‘位置。


题目练习


http://www.itpub.net/thread-2072122-1-1.html


哪些选项实现了一个名为plch_between_2_4的函数,使得下列代码块执行之后会显示 "enXFeue" ?


BEGINsys.DBMS_OUTPUT.put_line (plch_between_2_4 ('StevenXFeuerstein', 'e'));
END;
/(A) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
IS
BEGINRETURN SUBSTR (string_in, INSTR (string_in, letter_in, 1, 2),   INSTR (string_in, letter_in, 1, 4)- INSTR (string_in, letter_in, 1, 2)+ 1);
END;
/(B) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
ISc_2nd   CONSTANT PLS_INTEGER:= INSTR (string_in, letter_in, 1, 2) ;c_4th   CONSTANT PLS_INTEGER:= INSTR (string_in, letter_in, 1, 4) ;
BEGINRETURN SUBSTR (string_in, c_2nd, c_4th - c_2nd + 1);
END;
/(C) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
ISl_2nd   PLS_INTEGER:= INSTR (string_in, letter_in, 1, 2);l_4th   PLS_INTEGER:= INSTR (string_in, letter_in, l_2nd + 1, 2);
BEGINRETURN SUBSTR (string_in, l_2nd, l_4th - l_2nd + 1);
END;
/(D) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
IS
BEGINRETURN SUBSTR (string_in, letter_in, 2, 4);
END;
/

A选项中相当于SUBSTR('StevenXFeuerstein', 5 , 11-5+1) 结果是enXFeue,BC选项其实和A选择表达的意思相同,D选项substr( string, start_position, [ length ] )参数超过了要求的所以报错,答案应该是ABC


这篇关于INSTR函数浅析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1122086

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT