sql instr函数_如何实现SQL INSTR()函数?

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)

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

sql instr函数

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值