sql instr函数

Hey, folks! In this article, we will be understanding SQL INSTR() function in detail.

嘿伙计! 在本文中,我们将详细了解SQL INSTR()函数



SQL INSTR()的工作 (Working of SQL INSTR())

SQL, being a query language, contains various in-built functions to deal with the String data values of the database.

作为一种查询语言, SQL包含各种内置函数来处理数据库的String数据值。

One such interesting function is SQL INSTR() function.

SQL INSTR()函数就是这样一种有趣的函数。

SQL INSTR() function detects the first occurrence of a string or a character in the other string. Thus, the INSTR() function witnesses the position of the initial/first occurrence of any string/sub-string in another string data value.

SQL INSTR() function检测一个字符串或另一个字符串中字符的首次出现。 因此,INSTR()函数见证了另一个字符串数据值中任何字符串/子字符串的首次出现/首次出现的位置。

Having understood the working of SQL INSTR() function, let us now go ahead with the structure and syntax of the same.

了解了SQL INSTR()函数的工作原理后,现在让我们继续介绍该结构和语法。



了解INSTR()的语法 (Understanding the Syntax of INSTR())

SQL INSTR() function accepts two parameters:

SQL INSTR()函数接受两个参数:

  • String/character to search for in the other String data value.在另一个String数据值中 搜索的字符串/字符
  • The string within which the occurrence of the character/string would be searched for.将在其中搜索字符/字符串出现的字符串。

INSTR(string1, string2);

The INSTR() function returns an integer value stating the index of the first occurrence of the string to be searched.

INSTR()函数返回一个整数值,该整数表示要搜索的字符串的首次出现的索引。

Now let us understand the implementation of the INSTR() function in the next section through various examples.

现在,通过各种示例,让我们在下一部分中了解INSTR()函数的实现。



通过示例实现SQL INSTR() (Implementing SQL INSTR() through examples)

In the below example, the SQL INSTR() function searches for the first occurrence of the character ‘P’ within the input string data value.

在下面的示例中,SQL INSTR()函数在输入字符串数据值中搜索字符“ P”的首次出现。


SELECT INSTR('JYPython', 'P');

Output:

输出:


3

Apart from searching for the first occurrence of characters within a string, INSTR() function works with string values as well.

除了搜索字符串中字符的第一个匹配项外,INSTR()函数还适用于字符串值。

Here, we have searched for the first occurrence of the string ‘JournalDev’ within the input data value and returns the position value of it.

在这里,我们已经在输入数据值中搜索字符串'JournalDev'的首次出现,并返回了它的位置值。


SELECT INSTR('Python@JournalDev', 'JournalDev');

Output:

输出:


8

In this example, we have created a table with different columns. We have tried to display the index of first occurrence of the character ‘a’ of each data value present in the column – ‘city’ of table – ‘Info’.

在此示例中,我们创建了具有不同列的表。 我们试图显示列中每个数据值的字符“ a”首次出现的索引–表的“城市” –“信息”。


create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Puna");
insert into Info(id, Cost,city) values(2, 50, "Satara");
insert into Info(id, Cost,city) values(3, 65,"Puna");
insert into Info(id, Cost,city) values(4, 97,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");
SELECT city, INSTR(city, "a") as 1st_Occurrence_of_a
FROM Info;

Output:

输出:


city    1st_Occurrence_of_a
Puna    4
Satara  2
Puna    4
Mumbai  5
USA 3

If the string/character to be searched for its first occurrence is not contained or present in the string, the INSTR() function returns zero (0).

如果字符串中不包含要搜索的字符串/字符,则INSTR()函数将返回零(0)。


SELECT INSTR('Python', 'xx');

Output:

输出:


0


结论 (Conclusion)

By this, we have come to the end of this topic. Please feel free to comment below in case you come across any doubts.

至此,我们到了本主题的结尾。 如果您有任何疑问,请随时在下面发表评论。

For more such posts related to SQL, please visit SQL JournalDev.

有关与SQL有关的更多此类帖子,请访问SQL JournalDev 。



参考资料 (References)

  • SQL INSTR() function — DocumentationSQL INSTR()函数— 文档

翻译自: https://www.journaldev.com/41270/sql-instr-function

sql instr函数

sql instr函数_如何实现SQL INSTR()函数?相关推荐

  1. mysql开窗函数_魔幻的SQL开窗函数,为您打开进阶高手的一扇天窗

    经常写SQL脚本的朋友,通常会有一种迷之自信,似乎各种问题都有自己的一套解决方案.时间长了,人的思维可能会逐渐固化.思维固化能提高工作效率,但从某些角度看是很可怕的,我们也同时会失去接受新知识的内在动 ...

  2. sql date 函数_什么是SQL DATE()函数?

    sql date 函数 Hey, folks! In this article, we will be focusing on SQL DATE() function in detail. So, l ...

  3. coalesce函数_什么是SQL Server COALESCE()函数?

    coalesce函数 Hey, folks! In this article, we will be focusing on SQL Server COALESCE() function. 嘿伙计! ...

  4. sql dateadd函数_什么是SQL Server DATEADD()函数?

    sql dateadd函数 Hey, folks! In this article, we will be focusing on SQL Server DATEADD() function in d ...

  5. mysql bitand函数_有趣的SQL(一)

    原标题:有趣的SQL(一) 大家都想提高自己的SQL能力,但是SQL能力也是需要慢慢提高的,为了让大家的SQL有所提高,特整理了下面的SQL. 需求:如下所示,有从1开始的2的幂的数组 - - 1, ...

  6. sql int 比较_高质量SQL的30条建议 建议你收藏

    前言 本文将结合实例demo,阐述30条有关于优化SQL的建议,多数是实际开发中总结出来的,希望对大家有帮助. 查询SQL尽量不要使用select *,而是select具体字段. 反例子: selec ...

  7. in ms sql 集合参数传递_神奇的 SQL → 为什么 GROUP BY 之后不能直接引用原表中的列?...

    GROUP BY 后 SELECT 列的限制 标准 SQL 规定,在对表进行聚合查询的时候,只能在 SELECT 子句中写下面 3 种内容:通过 GROUP BY 子句指定的聚合键.聚合函数(SUM ...

  8. sql 删除依赖_关系数据库标准语言SQL(二)

    声明:最近在准备考试,故整理数据库原理笔记. 视图 视图的创建和删除 CREATE VIEW <视图名 > [ <列名 > , --, <列名 >)] AS &l ...

  9. 刚装的系统没有sql server(mssqlserver)_数据库与SQL学习

    本篇是数据分析系统学习专栏的第四篇文章--数据库与SQL学习.如果想要了解写作初衷,可以先行阅读如何系统学习数据分析. 数据库和SQL的基本概念 推荐阅读:怎么简单地理解数据库的概念? 核心概念: 数 ...

最新文章

  1. 双轴机械臂串口控制命令开发与测试:STM32F103控制板,简易调试命令集合
  2. 应对电信劫持强行插入广告的处理
  3. try catch 有多烦人,我就有多暴躁!一次搞定 Exception ^ ^
  4. Java基础 抽象类和接口的区别、equals 与 == 的区别
  5. C++数组(一维、二维、三维)的动态分配new、初始化memset和释放delete
  6. sourcetree 拉取 一直让输入密码
  7. 学习Spring Boot:(二十一)使用 EhCache 实现数据缓存
  8. Codeforces 570D Tree Requests(树上启发式合并)
  9. MySQL5.7更改密码时出现ERROR 1054
  10. 【vue】路由Router基础详解,带你快速入门~
  11. 浅谈-LINUX 操作系统启动过程
  12. 圣殿骑士的一篇关于WPF的培训好文,WPF应用与团队开发(转)
  13. 软件项目开发流程以及人员职责,软件工程中五种常用的软件开发模型整理
  14. 研究生看论文的看的是什么
  15. Enchanted —— Taylor Swift
  16. 室内全景图数据集的设计及制作流程
  17. 计算机网络协议(三)——UDP、TCP、Socket
  18. 〖大前端 - 基础入门三大核心之CSS篇④〗- CSS选择器之元素关系选择器、序号选择器与属性选择器
  19. Python爬虫:调用百度翻译接口实现中英翻译功能
  20. 我奋斗了十八年才和你坐在一起喝咖啡

热门文章

  1. Android网络应用的封装与调度
  2. [转载] 递归函数python基例_python递归函数详解 python 递归函数使用装饰器
  3. [转载] python自定义异常类型和raise抛出异常
  4. [转载] python中断响应_用Python脚本监测.py脚本的进程状态,并实现中断重启。
  5. 39. 组合总和 ,40. 组合总和 II,216. 组合总和 III
  6. 【leetcode 简单】第四十一题 Excel表列序号
  7. WPF Demo20 模板
  8. float/文档流/清除浮动
  9. 【转载】Objective-C runtime 消息机制
  10. WIZnet推出串口转以太网模块WIZ550S2E