oracle本身没有提供直接的分割函数,需要我们自定义一个:
--创建类型
CREATE OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000);
--创建函数
CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN str_split
PIPELINED
AS
v_length NUMBER := LENGTH(p_string);
v_start NUMBER := 1;
v_index NUMBER;
BEGIN
WHILE(v_start <= v_length)
LOOP
v_index := INSTR(p_string, p_delimiter, v_start);
IF v_index = 0
THEN
PIPE ROW(SUBSTR(p_string, v_start));
v_start := v_length + 1;
ELSE
PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start));
v_start := v_index + 1;
END IF;
END LOOP;
RETURN;
END splitstr;
--使用函数 已',' 分割字符串 '01,02,03,04,05,06,15'
select * from table(splitstr('01,02,03,04,05,06,15',','));
使用分割函数将字符串 ‘01,02,03,04,05,06,15’ 分割成 ‘01’,‘02’,‘03’,‘04’,‘05’,‘06’,‘15’ ,从而实现 in(‘01’,‘02’,‘03’,‘04’,‘05’,‘06’,‘15’)的判断效果
select *
from PERSONNEL_BASE
where 1 = 1
and work_type in
(select *
from table(splitstr(
case
when indate < to_date('2019-12-25', 'yyyy-mm-dd') then
'03,07,15'
else
'01,02,03,04,05,06,15'
end,
',')))
这点没有mysql处理起来方便:
where 1=1
and case when outdate < str_to_date('2019-12-25', '%Y-%m-%d')
then work_type in('03','07','15')
else work_type in('01','02','03','04','05','06','15')
end