split_part

Description

Syntax

VARCHAR split_part(VARCHAR content, VARCHAR delimiter, INT field)

Returns the specified partition by splitting the string according to the delimiter. If field is positive, splitting and counting from the beginning of content, otherwise from the ending.

delimiter and field parameter should be constant.

example

  1. mysql> select split_part("hello world", " ", 1);
  2. +----------------------------------+
  3. | split_part('hello world', ' ', 1) |
  4. +----------------------------------+
  5. | hello |
  6. +----------------------------------+
  7. mysql> select split_part("hello world", " ", 2);
  8. +----------------------------------+
  9. | split_part('hello world', ' ', 2) |
  10. +----------------------------------+
  11. | world |
  12. +----------------------------------+
  13. mysql> select split_part("2019年7月8号", "月", 1);
  14. +-----------------------------------------+
  15. | split_part('2019年7月8号', '月', 1) |
  16. +-----------------------------------------+
  17. | 20197 |
  18. +-----------------------------------------+
  19. mysql> select split_part("abca", "a", 1);
  20. +----------------------------+
  21. | split_part('abca', 'a', 1) |
  22. +----------------------------+
  23. | |
  24. +----------------------------+
  25. mysql> select split_part("prefix_string", "_", -1);
  26. +--------------------------------------+
  27. | split_part('prefix_string', '_', -1) |
  28. +--------------------------------------+
  29. | string |
  30. +--------------------------------------+
  31. mysql> select split_part("prefix_string", "_", -2);
  32. +--------------------------------------+
  33. | split_part('prefix_string', '_', -2) |
  34. +--------------------------------------+
  35. | prefix |
  36. +--------------------------------------+
  37. mysql> select split_part("abc##123###234", "##", -1);
  38. +----------------------------------------+
  39. | split_part('abc##123###234', '##', -1) |
  40. +----------------------------------------+
  41. | 234 |
  42. +----------------------------------------+
  43. mysql> select split_part("abc##123###234", "##", -2);
  44. +----------------------------------------+
  45. | split_part('abc##123###234', '##', -2) |
  46. +----------------------------------------+
  47. | 123# |
  48. +----------------------------------------+

keywords

  1. SPLIT_PART,SPLIT,PART