split_part

description

Syntax

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

根据分割符拆分字符串, 返回指定的分割部分(从一或负一开始计数)。field字段支持负数,代表从右往左倒着截取并取数。 delimiterfield 参数需要是常量, 不支持变量。

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