split_part
description
Syntax
VARCHAR split_part(VARCHAR content, VARCHAR delimiter, INT field)
根据分割符拆分字符串, 返回指定的分割部分(从一或负一开始计数)。field字段支持负数,代表从右往左倒着截取并取数。 delimiter 和 field 参数需要是常量, 不支持变量。
example
1. mysql> select split_part("hello world", " ", 1);
2. +----------------------------------+
3. | split_part('hello world', ' ', 1) |
4. +----------------------------------+
5. | hello |
6. +----------------------------------+
9. mysql> select split_part("hello world", " ", 2);
10. +----------------------------------+
11. | split_part('hello world', ' ', 2) |
12. +----------------------------------+
13. | world |
14. +----------------------------------+
16. mysql> select split_part("2019年7月8号", "月", 1);
17. +-----------------------------------------+
18. | split_part('2019年7月8号', '月', 1) |
19. +-----------------------------------------+
20. | 2019年7 |
21. +-----------------------------------------+
23. mysql> select split_part("abca", "a", 1);
24. +----------------------------+
25. | split_part('abca', 'a', 1) |
26. +----------------------------+
27. | |
28. +----------------------------+
30. mysql> select split_part("prefix_string", "_", -1);
31. +--------------------------------------+
32. | split_part('prefix_string', '_', -1) |
33. +--------------------------------------+
34. | string |
35. +--------------------------------------+
37. mysql> select split_part("prefix_string", "_", -2);
38. +--------------------------------------+
39. | split_part('prefix_string', '_', -2) |
40. +--------------------------------------+
41. | prefix |
42. +--------------------------------------+
44. mysql> select split_part("abc##123###234", "##", -1);
45. +----------------------------------------+
46. | split_part('abc##123###234', '##', -1) |
47. +----------------------------------------+
48. | 234 |
49. +----------------------------------------+
51. mysql> select split_part("abc##123###234", "##", -2);
52. +----------------------------------------+
53. | split_part('abc##123###234', '##', -2) |
54. +----------------------------------------+
55. | 123# |
56. +----------------------------------------+
keywords
1. SPLIT_PART,SPLIT,PART
