Split function in mysql

Source: Internet
Author: User
There is no split function in mysql. You need to write it yourself: 1) obtain the number of strings separated by specified characters: SQL code DELIMITER $ DROPFUNCTIONIFEXISTS 'sims '. 'func _ get_split_string_total '$ CREATEDEFINER 'root' @ 'localhost' FUNCTION 'func _ get_split_string_total' (f_strin

There is no split FUNCTION in mysql. You need to write it yourself: 1) obtain the number of strings separated by specified characters: SQL code DELIMITER $ DROP FUNCTION IFEXISTS 'sims '. 'func _ get_split_string_total '$ create definer = 'root' @ 'localhost' FUNCTION 'func _ get_split_string_total' (f_strin

There is no split function in mysql and you need to write it yourself:

1) obtain the number of strings separated by specified characters:

SQL code

  1. DELIMITER $
  2. Drop function if exists 'sims'. 'func _ get_split_string_total '$
  3. Create definer = 'root' @ 'localhost' FUNCTION 'func _ get_split_string_total '(
  4. F_string varchar (1000), f_delimiter varchar (5)
  5. ) RETURNS int (11)
  6. BEGIN
  7. Declare returnInt int (11 );
  8. If length (f_delimiter) = 2 then
  9. Return 1 + (length (f_string)-length (replace (f_string, f_delimiter, '')/2;
  10. Else
  11. Return 1 + (length (f_string)-length (replace (f_string, f_delimiter ,'')));
  12. End if;
  13. END $
  14. DELIMITER;

For example, func_get_split_string_total ('abc | def | gh', '|') returns 3.

2) returns the string after the I-th segment.

SQL code

  1. DELIMITER $
  2. Drop function if exists 'sims'. 'func _ get_split_string '$
  3. Create definer = 'root' @ 'localhost' FUNCTION 'func _ get_split_string '(
  4. F_string varchar (1000), f_delimiter varchar (5), f_order int) RETURNS varchar (255) CHARSET utf8
  5. BEGIN
  6. Declare result varchar (255) default '';
  7. Set result = reverse (substring_index (f_string, f_delimiter, f_order), f_delimiter, 1 ));
  8. Return result;
  9. END $
  10. DELIMITER;

For example: func_get_split_string ('abc | def | gh', '|', 2) is def

3) Requirement: A field value in Table a is 1 | 2. In the select statement, A and B must be obtained through association with the B dictionary table.

SQL code

  1. Create definer = 'root' @ 'localhost' FUNCTION 'getdictname' (v_str varchar (100) RETURNS varchar (100) CHARSET utf8
  2. BEGIN
  3. DECLARE I int (4 );
  4. DECLARE dictCode varchar (100 );
  5. DECLARE dictName varchar (100 );
  6. DECLARE returnStr varchar (100 );
  7. Set I = 1;
  8. Set returnStr = '';
  9. If (v_str is null or length (v_str) = 0) then
  10. Return returnStr;
  11. Else
  12. While I <= func_get_split_string_total (v_str, '| ')
  13. Do
  14. Set dictCode = func_get_split_string (v_str, '|', I );
  15. Select names into dictName from sims_dd_dict where code = dictCode;
  16. Set returnStr = concat (returnStr, ',', dictName); -- use a comma (,) in Chinese. Otherwise, the data is serialized during EXCEL export because the program is separated by commas (,).
  17. Set I = I + 1;
  18. End while;
  19. Set returnStr = subString (returnStr, 2 );
  20. Return returnStr;
  21. End if;
  22. END $

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.