Implementing split_part Function in GBase 8s: Two Practical Approaches
GBase 8s does not provide a built‑in split_part function like PostgreSQL. However, using the existing SUBSTRING_INDEX function or manual character parsing, we can easily achieve the same functional...

Source: DEV Community
GBase 8s does not provide a built‑in split_part function like PostgreSQL. However, using the existing SUBSTRING_INDEX function or manual character parsing, we can easily achieve the same functionality. This article introduces two implementation methods with practical examples. What Does split_part Do? The split_part function splits a string by a delimiter and returns the n‑th field. Example: split_part('aa,bb,cc,dd', ',', 3) → 'cc' Method 1: Using SUBSTRING_INDEX GBase 8s provides SUBSTRING_INDEX, which extracts a substring up to a specified number of occurrences. This makes the implementation very straightforward. DROP FUNCTION IF EXISTS split_part2; CREATE FUNCTION split_part2( str_in lvarchar(2048), separator_in CHAR(1), field_in INT ) RETURNING VARCHAR(255); DEFINE str_len INT; DEFINE pos_curr INT; DEFINE count_field INT; DEFINE pos_char CHAR(1); IF field_in <= 0 THEN RETURN NULL; END IF; LET count_field = 1; LET str_len = LENGTH(str_in); -- Count delimiters to know total fields