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))
asbegin
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)
endif(@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;
查詢**:
#測試是否能成功分割
#select * from tmp_split;
split 將字串分割成字串陣列
list name list name.split split 方法用於把乙個字串分割成字串陣列。stringobject.split separator,howmany 引數 描述separator 必需。字串或正規表示式,從該引數指定的地方分割 stringobject。howmany 可選。該...
shell 將字串分割成陣列
shell程式設計中,經常需要將由特定分割符分割的字串分割成陣列,多數情況下我們首先會想到使用awk 但是實際上用shell自帶的分割陣列功能會更方便。假如 a one,two,three,four 要將 a分割開,可以這樣 old ifs ifs ifs arr a ifs old ifs for...
SQL字串分割成若干列
在資料庫程式設計中,很多朋友會碰到分割字串的需求,一般都是分割成一列多行模式,但也有時會需要分割成多列一行的模式,下面我們來看下如何實現這種需求。首先建立乙個輔助函式,來得到生成多列的sql語句 create function toarray str nvarchar 1000 sym nvarch...