Oracle SQL 字符分割
2012-07-13 18:35
162 查看
select get_carton_list_split(',2,4,5,7,9,10,12,13,14') from dual;
得到如下结果:
2,4~5,7,9~10,12~14
下面是用到的自定义函数,以供参考!
create or replace type tb_strSplit as 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;
create or replace function get_carton_list_split (
carton_list in varchar2
)
return varchar as
v_carton_list_split varchar2(2000);
begin
declare
value number;
diff number;
previousvalue number;
nextvalue number;
ret varchar2(2000);
cursor c_split is
select value,diff,nvl(lag(value)over(order by value),0) previousvalue,nvl(lead(value)over(order by value),0) nextvalue from (
select to_number(column_value) value,nvl(to_number(column_value)-lag(to_number(column_value)) over (order by 1),0) diff
from table(splitstr(carton_list,',')) where column_value is not null order by to_number(column_value)) ;
begin
open c_split;
loop
fetch c_split into value,diff,previousvalue,nextvalue;
exit when c_split%notfound;
ret:=ret|| (case
when diff=0 and nvl(nextvalue,0)=0 then to_char(value)
when diff=0 and nextvalue-value=1 then to_char(value)||'~'
when diff=0 and nextvalue-value>1 then to_char(value)||','
when diff>1 and nextvalue-value=1 and value-nvl(previousvalue,0)>1 then to_char(value)||'~'
when diff>1 and nextvalue-value=1 and value-nvl(previousvalue,0)=1 then ''
when diff>1 and nextvalue-value>1 and value-nvl(previousvalue,0)>1 then to_char(value)||','
when diff>1 and nvl(nextvalue,0)=0 then to_char(value)
when diff>1 and nextvalue-value>1 then ','
when diff=1 and nextvalue-value>1 then to_char(value)||','
when diff=1 and nvl(previousvalue,0)=0 then ''
when diff=1 and nextvalue-value=1 then ''
when diff=1 and nvl(nextvalue,0)=0 then to_char(value)
else ',' end);
end loop;
v_carton_list_split:=ret;
close c_split;
end;
return v_carton_list_split;
exception
when others then
return null;
end;
得到如下结果:
2,4~5,7,9~10,12~14
下面是用到的自定义函数,以供参考!
create or replace type tb_strSplit as 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;
create or replace function get_carton_list_split (
carton_list in varchar2
)
return varchar as
v_carton_list_split varchar2(2000);
begin
declare
value number;
diff number;
previousvalue number;
nextvalue number;
ret varchar2(2000);
cursor c_split is
select value,diff,nvl(lag(value)over(order by value),0) previousvalue,nvl(lead(value)over(order by value),0) nextvalue from (
select to_number(column_value) value,nvl(to_number(column_value)-lag(to_number(column_value)) over (order by 1),0) diff
from table(splitstr(carton_list,',')) where column_value is not null order by to_number(column_value)) ;
begin
open c_split;
loop
fetch c_split into value,diff,previousvalue,nextvalue;
exit when c_split%notfound;
ret:=ret|| (case
when diff=0 and nvl(nextvalue,0)=0 then to_char(value)
when diff=0 and nextvalue-value=1 then to_char(value)||'~'
when diff=0 and nextvalue-value>1 then to_char(value)||','
when diff>1 and nextvalue-value=1 and value-nvl(previousvalue,0)>1 then to_char(value)||'~'
when diff>1 and nextvalue-value=1 and value-nvl(previousvalue,0)=1 then ''
when diff>1 and nextvalue-value>1 and value-nvl(previousvalue,0)>1 then to_char(value)||','
when diff>1 and nvl(nextvalue,0)=0 then to_char(value)
when diff>1 and nextvalue-value>1 then ','
when diff=1 and nextvalue-value>1 then to_char(value)||','
when diff=1 and nvl(previousvalue,0)=0 then ''
when diff=1 and nextvalue-value=1 then ''
when diff=1 and nvl(nextvalue,0)=0 then to_char(value)
else ',' end);
end loop;
v_carton_list_split:=ret;
close c_split;
end;
return v_carton_list_split;
exception
when others then
return null;
end;
相关文章推荐
- ORACLE(PL/SQL) 根据字符分割(SPLIT)字符串返回数组
- PL SQL Developer登陆Oracle提示客户端与数据库字符编码设置不一致
- Oracle中把逗号分割的字符串转换为可放入in的条件语句的字符数列
- oracle sql模糊查询时允许查询特殊字符
- Oracle支持的字符函数和它们的Microsoft SQL Server等价函数
- sql:字符串按照某个字符分割后取第几个字符串
- 【Oracle】PL/SQL提取一段字符串中的每个字符
- SQL实现split函数,自定义分割字符,自定义取出第几个分割字符前的字符串
- 关于执行Oracle下Sql语句中遇到的特殊字符问题解决办法。
- Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列
- sql中字符分割,日期判断,以及函数的应用
- SQL 用指定的字符,分割字符串
- 关于SQL文件一放到OracleSqlDeveloper中文字符就出现乱码的情况
- 【Oracle】 sql 中的字符(串)替换与转换
- 【总结】Oracle sql 中的字符(串)替换与转换
- Hibernate createSQLQuery 查询Oracle char类型结果为一个字符解决方法汇总
- SQL-Oracle异常--java.sql.SQLDataException: ORA-01861: 文字与格式字符串不匹配
- SQL 把字符创分割成两个字符串
- 分割字符-SQL语句之逗号
- Oracle Sql插入数据中特殊字符的处理