array_join

array_join

description

Syntax

VARCHAR array_join(ARRAY<T> arr, VARCHAR sep[, VARCHAR null_replace])

Combines all elements in the array to generate a new string according to the separator (sep) and the string to replace NULL (null_replace). If sep is NULL, return NULL. If null_replace is NULL, return NULL. If sep is an empty string, no delimiter is applied. If null_replace is an empty string or not specified, the NULL elements in the array are discarded directly.

example

  1. mysql> select k1, k2, array_join(k2, '_', 'null') from array_test order by k1;
  2. +------+-----------------------------+------------------------------------+
  3. | k1 | k2 | array_join(`k2`, '_', 'null') |
  4. +------+-----------------------------+------------------------------------+
  5. | 1 | [1, 2, 3, 4, 5] | 1_2_3_4_5 |
  6. | 2 | [6, 7, 8] | 6_7_8 |
  7. | 3 | [] | |
  8. | 4 | NULL | NULL |
  9. | 5 | [1, 2, 3, 4, 5, 4, 3, 2, 1] | 1_2_3_4_5_4_3_2_1 |
  10. | 6 | [1, 2, 3, NULL] | 1_2_3_null |
  11. | 7 | [4, 5, 6, NULL, NULL] | 4_5_6_null_null |
  12. +------+-----------------------------+------------------------------------+
  13. mysql> select k1, k2, array_join(k2, '_', 'null') from array_test01 order by k1;
  14. +------+-----------------------------------+------------------------------------+
  15. | k1 | k2 | array_join(`k2`, '_', 'null') |
  16. +------+-----------------------------------+------------------------------------+
  17. | 1 | ['a', 'b', 'c', 'd'] | a_b_c_d |
  18. | 2 | ['e', 'f', 'g', 'h'] | e_f_g_h |
  19. | 3 | [NULL, 'a', NULL, 'b', NULL, 'c'] | null_a_null_b_null_c |
  20. | 4 | ['d', 'e', NULL, ' '] | d_e_null_ |
  21. | 5 | [' ', NULL, 'f', 'g'] | _null_f_g |
  22. +------+-----------------------------------+------------------------------------+
  23. mysql> select k1, k2, array_join(k2, '_') from array_test order by k1;
  24. +------+-----------------------------+----------------------------+
  25. | k1 | k2 | array_join(`k2`, '_') |
  26. +------+-----------------------------+----------------------------+
  27. | 1 | [1, 2, 3, 4, 5] | 1_2_3_4_5 |
  28. | 2 | [6, 7, 8] | 6_7_8 |
  29. | 3 | [] | |
  30. | 4 | NULL | NULL |
  31. | 5 | [1, 2, 3, 4, 5, 4, 3, 2, 1] | 1_2_3_4_5_4_3_2_1 |
  32. | 6 | [1, 2, 3, NULL] | 1_2_3 |
  33. | 7 | [4, 5, 6, NULL, NULL] | 4_5_6 |
  34. +------+-----------------------------+----------------------------+
  35. mysql> select k1, k2, array_join(k2, '_') from array_test01 order by k1;
  36. +------+-----------------------------------+----------------------------+
  37. | k1 | k2 | array_join(`k2`, '_') |
  38. +------+-----------------------------------+----------------------------+
  39. | 1 | ['a', 'b', 'c', 'd'] | a_b_c_d |
  40. | 2 | ['e', 'f', 'g', 'h'] | e_f_g_h |
  41. | 3 | [NULL, 'a', NULL, 'b', NULL, 'c'] | a_b_c |
  42. | 4 | ['d', 'e', NULL, ' '] | d_e_ |
  43. | 5 | [' ', NULL, 'f', 'g'] | _f_g |
  44. +------+-----------------------------------+----------------------------+

keywords

ARRAY, JOIN, ARRAY_JOIN