sql instr函數_如何實現SQL INSTR()函數?

 2023-11-19 阅读 14 评论 0

摘要: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 la

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 — Documentation

    SQL INSTR()函數— 文檔

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

sql instr函數

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/183280.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息