About oracle use find_in_set function

the cover

Original link: about oracle using find_in_set function | Elvin

1. Overview of find_in_set function

find_in_set英文逗号The function can retrieve the value (string) separated by a certain item in the data table .

find_in_set(str, strlist), str: the string to be queried; strlist: 英文逗号the string separated by

Different from MySQL?

Mysql comes with the find_in_set function, which can be used directly; oracle does not have this function, so we need to manually create the find_in_set function

2. How to query data with find_in_set function

As shown in the figure, there is a column named version_type in the table, and its values ​​are composed of (version_type:1,2,3) strings separated by English commas.

// 在mysql中,我们可以使用find_in_set函数进行精准搜索,如下

select *  from test where find_in_set('1',version_type);

// 这条语句可以查询出[列version_type]包含1的数据行

Why not use in or like?

The reason for not using in: because when using in, the column value of version_type must be equal to 1 to query the data;

The reason for not using like: If you use like, its matching method is more extensive. In the search, as long as this character is included, it will be returned. We need to get the value of 1 accurately. Like will put 1 in 11, 21, 111, etc. The values ​​all match, so it cannot be like

find_in_set function

It can only search for specific values ​​separated by commas, which is more precise than like, and must be in the form of 1 or 2 or 3, so the find_in_set function is more suitable when you need to get a value containing 1.

3. Create the find_in_set function in oracle

CREATE OR REPLACE FUNCTION FIND_IN_SET(piv_str1 varchar2,  piv_str2 varchar2,  p_sep varchar2 := ',')

The default is separated by commas, and can also be replaced by other characters. like:p_sep varchar2 := '|'

⚠️: 如果是换成其它字符隔开,则查询时需要传入隔开的字符,详细请看How to use oracle

The sql to create the function is as follows:

CREATE OR REPLACE FUNCTION FIND_IN_SET(piv_str1 varchar2, piv_str2 varchar2, p_sep varchar2 := ',')

RETURN NUMBER IS      
  l_idx    number:=0; -- 用于计算piv_str2中分隔符的位置  
  str      varchar2(500);  -- 根据分隔符截取的子字符串  
  piv_str  varchar2(500) := piv_str2; -- 将piv_str2赋值给piv_str  
  res      number:=0; -- 返回结果  
  loopIndex number:=0;

BEGIN  

-- 如果piv_str中没有分割符,直接判断piv_str1和piv_str是否相等,相等 res=1  
IF instr(piv_str, p_sep, 1) = 0 THEN  

   IF piv_str = piv_str1 THEN   
      res:= 1;  
   END IF;  

ELSE  

-- 循环按分隔符截取piv_str  
LOOP  
    l_idx := instr(piv_str,p_sep);  
     loopIndex:=loopIndex+1;

-- 当piv_str中还有分隔符时  
      IF l_idx > 0 THEN  

   -- 截取第一个分隔符前的字段str  
         str:= substr(piv_str,1,l_idx-1);  

   -- 判断 str 和piv_str1 是否相等,相等 res=1 并结束循环判断  
         IF str = piv_str1 THEN   
           res:= loopIndex;  
           EXIT;  

         END IF;  
        piv_str := substr(piv_str,l_idx+length(p_sep));  

      ELSE  

   -- 当截取后的piv_str 中不存在分割符时,判断piv_str和piv_str1是否相等,相等 res=1  
        IF piv_str = piv_str1 THEN   
           res:= loopIndex;  
        END IF;  

        -- 无论最后是否相等,都跳出循环  
        EXIT;  
      END IF;  

END LOOP;  

-- 结束循环  
END IF;  

-- 返回res  
RETURN res;  
END FIND_IN_SET;

4. How to use

The data is shown as follows:

1. In MySQL

The method of use is as follows:

select *  from test where find_in_set('2',version_type);

// 查询列VERSION_TYPE包含2的数据行
// 查询结果为2行

2. In oracle

The method of use is as follows:

select find_in_set('1','1,2,3,4')  from test
select * from test where find_in_set('1',version_type) > 0
  
// 查询列VERSION_TYPE包含1的数据行
// 查询结果为3行

If the function uses other delimiters, the delimiter must be passed in as follows:

select * from test where find_in_set('1',version_type,'|') > 0
  
// 查询列VERSION_TYPE包含1的数据行(数据由”|“分隔)
// 查询结果为3行

5. Links for reference

oracle FIND_IN_SET function

Use of find_in_set() function in mysql


More knowledge is being continuously updated!!!


statement

The source of the original text is indicated in the reference part, and the source of the original text can be obtained at the reference link of the article

If the content in the article involves the original copyright, please contact [email protected] by email , and the related articles or content will be changed or canceled in time

Guess you like

Origin blog.csdn.net/weixin_42464282/article/details/130990306