MySQL intercepts the content between two specified strings

Source: Internet
Author: User

For example, if the existing string "[] aseabcd [12345] ddxabcdsx []" is to be intercepted, the content between "ABCD [" and "first"] "after" ABCD ["" 12345 ", of course, the content length is not fixed, it can be "123456" or other strings.
When he asked me, I first thought about indexof. Later I checked and found that there was no indexof but locate in MySQL.
After more than half an hour of trying, we 'd better help him achieve this effect.

CopyCode The Code is as follows: Create procedure sp_str
(
In p_str varchar (50),/* original string */
In p_begin_str varchar (50),/* Start string to be matched */
In p_end_str varchar (50)/* end string to be matched */
Out p_result varchar (50)/* return result */
Not deterministic
SQL Security definer
Comment''
Begin
Declare m_len int default 0;
Declare m_index int default 0;
/* Calculate the index position of the first matching string */
Select locate (p_begin_str, p_str) + char_length (p_begin_str) into m_index;
/* Calculate the length of the first matching string */
Select locate (p_end_str, p_str, m_index) into m_len;
Select substring (p_str, m_index, m_len-m_index) into p_result;
End;

Run:
call sp_str ('[] ABCD [12345] AA [] ss', 'abcd [', ']', @ result );
return value @ result 12345
call sp_str ('[] ABCD [sdww] AA [] ss', 'abcd [', ']', @ result );
return value @ result is sdww
if no stored procedure is required, you can directly write an SQL statement:
example: copy Code the code is as follows: select substring (
'] ABCD [12345] 111 []',
locate ('abcd [','] ABCD [12345] 111 [] ') + char_length ('abcd ['),
locate ('] ','] ABCD [12345] 111 [] ', char_length ('abcd [')) -
(select locate ('abc [','] ABCD [12345] 111 [] ') + char_length ('abcd [')
)

return value: 12345
about MySQL functions:
char_length (STR)
returns the length of the STR string.
locate (substr, STR)
position (substr in Str)
returns the position where the substring substr appears first in the STR string, if substr is not in STR, 0 is returned.
mysql> select locate ('bar', 'foobar');
-> 4
mysql> select locate ('xbar', 'foobar ');
-> 0
this function is multi-byte reliable. Locate (substr, STR, POS)
returns the position where the substring substr appears first in the STR string, starting from the position POS. If substr is not in STR, 0 is returned.
mysql> select locate ('bar', 'foobarbar', 5);
-> 7
this function is multi-byte reliable.
substring (STR, POs, Len)
substring (STR from POS for Len)
mid (STR, POs, Len)
return a substring of Len characters from the STR string, starting from the position POS. The variant form of from is ANSI sql92 syntax.
mysql> select substring ('quadratically ', 5, 6);
-> 'ratica'
this function is reliable in multiple bytes.
substring (STR, POS)

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.