2019-05-02

MySQL - Split string into pieces delimited by another string, and retrieve only the 'nth' element

select
-- Original field value
original_value

-- Second element, without delimiter
, REPLACE(
-- Second element, including its delimiter
REPLACE(
-- Desired element with all previous ones
SUBSTRING_INDEX( original_value, ',', 2)
-- All previous elements
, SUBSTRING_INDEX( original_value, ',', 1)
, ''
)
, ','
, ''
) as _second_element

-- Fourth element
, REPLACE(
-- Desired element with all previous ones
SUBSTRING_INDEX( original_value, ',', 4)
-- All previous elements
, SUBSTRING_INDEX( original_value, ',', 3)
, ''
) as _fourth_element
from
(select 'apple jelly and toast,bbq chicken,celery logs,daisies-bouquet,ebony,f-key,g-string,ivory' as original_value) as n1
;


No comments:

Post a Comment