1. Custom split function script
CREATE DEFINER = `root`@`%` FUNCTION `tjdemo`.`fun_get_split_string_total`(f_string varchar(1000),f_delimiter varchar(5))
RETURNS int(11)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
declare returnInt int(11);
if length(f_delimiter)=2 then
return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')))/2;
else
return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')));
end if;
END
Transfer from: mysql split function usage example
Description:
tjdemo: database name
fun_get_split_string_total: function name
f_string: input parameter, the string to be split
f_delimiter: delimiter string
Note: only the function's splitter character string length only supports 1 to 2 digits.
If the delimiter string exceeds 2 digits, use the REPLACE function to first replace the delimiter string in the split string with a 1-digit or 2-digit delimiter string.
The following statement cannot be executed:
select fun_get_split_string_total('aaa#option#cc#option#ddd#option#ff','#option#') as num;
If you want to achieve it, you can use REPLACE, as follows:
select fun_get_split_string_total(REPLACE('aaa#option#cc#option#ddd#option#ff','#option#','|'),'|') as num;