--
DELIMITER $$
CREATE FUNCTION `func_get_split_string_total`(
f_string varchar(1000),f_delimiter varchar(5)
) RETURNS int(11)
BEGIN
-- Get the total number of given string.
return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')));
END$$
DELIMITER ;
--
DELIMITER $$
CREATE FUNCTION `func_get_split_string`(
f_string varchar(1000),f_delimiter varchar(5),f_order int) RETURNS varchar(255) CHARSET utf8
BEGIN
-- Get the separated number of given string.
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$$
DELIMITER ;
--
DELIMITER $$
CREATE PROCEDURE `sp_print_result`(
IN f_string varchar(1000),IN f_delimiter varchar(5)
)
BEGIN
-- Get the separated string.
declare cnt int default 0;
declare i int default 0;
set cnt = func_get_split_string_total(f_string,f_delimiter);
drop table if exists tmp_print;
create temporary table tmp_print (num int not null);
while i < cnt
do
set i = i + 1;
insert into tmp_print(num) values (func_get_split_string(f_string,f_delimiter,i));
end while;
select * from tmp_print;
END$$
DELIMITER ;
--
call sp_print_result("1,3,2,56,888",",");
说明:前二个执行是函数,最后一个是存储过程
标签:varchar,string,--,一行行,DELIMITER,逗号,delimiter,拆分,print From: https://www.cnblogs.com/jk200/p/17217787.html