Mysql function Split function realizes _mysql

Source: Internet
Author: User
Set first: Set GLOBAL log_bin_trust_function_creators = 1;

1. Function Func_splitstringtotal: Split the string in the specified way and calculate the total number of cells

Copy Code code 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, getting the number of the specified position

Copy Code code 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 (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. Process splitstring The string and puts it in the temporary table Tmp_split

Copy Code code as follows:

DELIMITER $$
DROP PROCEDURE IF EXISTS ' splitstring ' $$
CREATE PROC Edure ' splitstring '
(in f_string varchar (1000), in F_delimiter varchar (5))

BEGIN
Declare cnt int de Fault 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.