日期:2014-05-16  浏览次数:20417 次

利用数据库将字符串分割成Table函数

1、Oracle中

创建代码:

create or replace type t_SplitTable as object
(
  id       integer,
  valueStr varchar2(8000)
)
;
?
create or replace type t_Split_Table as table of t_SplitTable;
?
create or replace function Fun_SplitByDot(p_str varchar2, p_char varchar2) return t_Split_Table as v_table t_Split_Table := t_Split_Table();

v_num_id integer; v_var_data varchar2(2000); v_num_index integer; begin

select nvl(p_str, '') into v_var_data from dual; if length(v_var_data) = 0 then return v_table; --字符长度为0 ,退出
end if;

v_num_id := 1;

select instr(v_var_data, p_char) into v_num_index from dual; while(v_num_index > 0) loop

v_table.extend(); v_table(v_table.count) := t_SplitTable(v_num_id, substr(v_var_data, 0, v_num_index - 1));

v_num_id := v_num_id + 1; select substr(v_var_data, v_num_index + 1, length(v_var_data) - v_num_index) into v_var_data from dual; select instr(v_var_data, p_char) into v_num_index from dual; end loop; if length(v_var_data) > 0 then v_table.extend(); v_table(v_table.count) := t_SplitTable(v_num_id, v_var_data); end if;

return v_table;

end Fun_SplitByDot;

查询:

select * from table(fun_splitbydot('a,2000001481,c',','));

?

2、SqlServer

创建代码:

CREATE FUNCTION StringToTable(@StringX varchar(8000),@Split nvarchar(10))
RETURNS @TableResult TABLE(TableID nvarchar(20))
AS
BEGIN
DECLARE @Index int
DECLARE @LenIndex int
SELECT @LenIndex=LEN(@Split),@Index=CHARINDEX(@Split,@StringX,1)
WHILE (@Index>=1)
BEGIN
INSERT INTO @TableResult SELECT LEFT(@StringX,@Index-1)
SELECT
@StringX=RIGHT(@StringX,LEN(@StringX)-@Index-@LenIndex+1),@Index=CHARINDEX(@Split,@StringX,1)
END
IF(@StringX<>'') INSERT INTO @TableResult SELECT @StringX
RETURN
END

? 查询:

select * from StringToTable('01__02__03','__') 
?

3、MySql

创建代码:

# 函数:func_split_TotalLength 
DROP function IF EXISTS `func_split_TotalLength` ;
CREATE FUNCTION `func_split_TotalLength` 
(f_string varchar(1000),f_delimiter varchar(5)) RETURNS int(11) 
BEGIN 
    # 计算传入字符串的总length 
    return 1+(length(f_string) - length(replace(f_string,f_delimiter,''))); 
END;
?

?

# 函数:func_split 
DROP function IF EXISTS `func_split` ;
CREATE FUNCTION `func_split` 
(f_string varchar(1000),f_delimiter varchar(5),f_order int) RETURNS varchar(255) CHARSET utf8 
BEGIN 
    # 拆分传入的字符串,返回拆分后的新字符串 
        declare result varchar(255) default ''; 
        set result = reverse(substring_index(reverse(substring_index(f_string,f_delimiter,f_order)),f_delimiter,1)); 
        return result; 
END;
?
# 存储过程:splitString 
DROP PROCEDURE IF EXISTS `splitString` ;
CREATE PROCEDURE `splitString` 
(IN f_string varchar(1000),IN f_delimiter varchar(5)) 
BEGIN 
# 拆分结果 
declare cnt int default 0; 
declare i int default 0; 
set cnt = func_split_TotalLength(f_string,f_delimiter); 
DROP TABLE IF EXISTS `tmp_split`; 
create temporary table `tmp_split` (`status` varchar(128) not null) DEFAULT CHARSET=utf8; 
while i < cnt 
do 
    set i = i + 1; 
    insert into tmp_split(`status`) values (func_split(f_string,f_delimiter,i)); 
end while; 
END;
?

查询代码:

#测试是否能成功分割
#call splitString("apple,banana,orange,pears,grape",","); 
#select * from tmp_split; 
?

?