Mysql function split

Source: Internet
Author: User

SET: set global log_bin_trust_function_creators = 1;

1. Function func_splitStringTotal: splits the string according to the specified method and calculates the total number of units.

Copy codeThe Code is as follows: DELIMITER $
Create function 'func _ get_splitstringtotal '(
F_string varchar (10000), f_delimiter varchar (50)
) RETURNS int (11)
BEGIN
Return 1 + (length (f_string)-length (replace (f_string, f_delimiter ,'')));
END $
DELIMITER;

2. Function func_splitString: splits the string in the specified way to obtain the number of specified positions.
Copy codeThe Code is as follows: DELIMITER $
DROP function if exists 'func_splitstring' $
Create function 'func _ splitstring'
(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 (f_string, f_delimiter, f_order), f_delimiter, 1 ));
Return result;
END $
SELECT func_splitString ('1, 2, 3, 4, 5, 6, 7 ', 1 );

3. Split the strings in the splitString process and put them in the tmp_split temporary table.
Copy codeThe Code is as follows: DELIMITER $
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 '('val _' varchar (128) not null) default charset = utf8;

While I <cnt
Do
Set I = I + 1;
Insert into tmp_split ('val _ ') values (func_split (f_string, f_delimiter, I ));
End while;
END $
Call splitString ('a, s, d, f, g, h, J ',',');
SELECT * from tmp_split;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.