Home > Mobile >  SUBSTRING_INDEX get nth value
SUBSTRING_INDEX get nth value

Time:04-20

I have this code:

SET @startEndTimeCapacity = "'00:00:00', '08:00:00', 120";
SELECT SUBSTRING_INDEX(@startEndTimeCapacity, ",", 2);

results in '00:00:00', '08:00:00'.

How can I get the 2nd value '08:00:00' only?

CodePudding user response:

You can apply twice SUBSTRING_INDEX:

  • for splitting at the nth value
  • for retrieving the last value in the previous list

Here's how:

SUBSTRING_INDEX(SUBSTRING_INDEX(@startEndTimeCapacity, ",", 2), ',', -1);

This works for any n. In this specific case n=2.

  • Related