Oracle implements compatibility with the find_in_set function in mysql

find_in_set(str, strList), find the position of str in strList.

-- return result is 1,  
SELECT FIND_IN_SET('1','1,2,3') FROM DUAL;  

 

-- return result is 2  
SELECT FIND_IN_SET('1','2,1,3') FROM DUAL;  

 The specific creation 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; -- used to calculate the position of the separator in piv_str2
  str varchar2(500); -- substring truncated by delimiter
  piv_str varchar2(500) := piv_str2; -- assign piv_str2 to piv_str
  res number:=0; -- return the result
  res_place number:=0;-- the position of the original string in the target string
BEGIN
-- returns 0 if the field is null
IF piv_str2 IS NULL THEN
  RETURN res;
END IF;
-- If there is no separator in piv_str, directly judge whether piv_str1 and piv_str are equal, equal res_place=1
IF instr(piv_str, p_sep, 1) = 0 THEN
   IF piv_str = piv_str1 THEN
      res_place:=1;
      res:= res_place;
   END IF;
ELSE
 -- Loop through the delimiter to intercept piv_str
LOOP
    l_idx := instr(piv_str,p_sep);
    --
    res_place := res_place + 1;
    -- when there is a separator in piv_str
      IF l_idx > 0 THEN
      -- Truncate the field str before the first delimiter
         str:= substr(piv_str,1,l_idx-1);
         -- Judging whether str and piv_str1 are equal, if they are equal, end the loop judgment
         IF str = piv_str1 THEN
           res:= res_place;
           EXIT;
         END IF;
        piv_str := substr(piv_str,l_idx+length(p_sep));
      ELSE
      -- When there is no delimiter in the intercepted piv_str, judge whether piv_str and piv_str1 are equal, equal res=res_path
        IF piv_str = piv_str1 THEN
           res:= res_place;
        END IF;
        -- Exit the loop regardless of whether it ends up being equal or not
        EXIT;
      END IF;
 END LOOP;
 -- end the loop
 END IF;
 -- return res
 RETURN res;
END FIND_IN_SET;

 For some queries, mysql only needs to write out the expression, but the oracle I wrote needs to write out the expression

E.g:
select * from tableA a left join tableB b on FIND_IN_SET(a.id ,b.aid) ; -- mysql can be written like this  
 
select * from tableA a left join tableB b on FIND_IN_SET(a.id ,b.aid) >0; -- oracle will write a specific expression  
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326654895&siteId=291194637