Friday, 15 September 2017

MySQL Split String Function

Pertama buat function nya dahulu

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');
jalankan querynya

SELECT SPLIT_STR(string, delimiter, position)
contoh pemakaian

Hasil
SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;

+-------+
| third |
+-------+
| ccc   |
+-------+

0 komentar:

Post a Comment