MySQL string segmentation implementation

Source: Internet
Author: User

How can I implement MySQL string segmentation? The following describes how to use MySQL to split strings. We hope that you can learn more about MySQL to split strings.

Example: update 'edw _ video' set company = substring (keyword, 1, length (SUBSTRING_INDEX (keyword, ",", 1 )))

Name: SPLIT_SUB_STR1 obtains the first segment of the split string)
Parameter: the input format of the target string to be split by str is '11, 6', and the output is '2 ')
Delimiter separator ',')
Split1 outputs the first item '11 ')
Stored Procedure
Create definer = 'root' @ '%' PROCEDURE 'split _ sub_str0' (inout str VARCHAR (1000), delimiter VARCHAR (1), out split1 VARCHAR (20 ))

BEGIN
# Name SPLIT_SUB_STR1 to obtain the first character of the separator string
# Parameter inout str VARCHAR (1000), delimiter VARCHAR (1), out split0 VARCHAR (20)
# Enter the complete string, and the output removes the string after obtaining the first item; the separator; the output is the first item of the split.

# Split the first string without the delimiter Length
DECLARE SUB_STR_LENGTH INT;

# The SUBSTRING_INDEX function obtains the left part of the nth delimiter on the left of the target string. If n is negative, the right part of the nth delimiter on the right is returned.
SET SUB_STR_LENGTH = length (SUBSTRING_INDEX (str, delimiter, 1 ));

# Truncate the first string, excluding the delimiter, and put it in the output parameter
SET split0 = substring (str, 1, SUB_STR_LENGTH );

# Obtain the string that removes the first string and separator, and obtain the Next string in the next loop
SET str = substring (str, SUB_STR_LENGTH + 2 );
# Test whether the output is correct
# SELECT SUB_STR_LENGTH, SUB_STR_SPLIT0, STR;
END

Call an instance

Create definer = 'root' @ '%' PROCEDURE 'test '()
BEGIN
# Target string
Set @ a = '1, 2, 3, 4, 5, 6, 12 ';
# Separator
Set @ c = ',';
# Stored string
Set @ B = '';

REPEAT
# Call the above Stored Procedure
CALL SPLIT_SUB_STR0 (@ a, ',', @ c );
# Concatenate the obtained string for testing
Set @ B = concat (@ B, @ c );
# Stop the loop when the target string is empty
UNTIL @ a =''
End repeat;
# View results
Select @ a, @ c, @ B;

END;

Replace string with the MySQL replace Function

How to query duplicate fields in a MySQL large table

Mysql query case

How to query duplicate records in MYSQL

Implementation of MySQL random Query

Related Article

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.