Question

Is it possible to add ellipsis after the MySQL substring_index function?

So if I have the following code:

SET @string = "abc, def, ghi, jkl, mno";

SELECT SUBSTRING_INDEX(@string, ', ', 3);

The result is: abc, def, ghi

So is it possible to add ... at the back ONLY when it is cut? I wish to retrieve this:

SELECT SUBSTRING_INDEX(@string, ', ', 3);
---> RESULT: abc, def, ghi...

SELECT SUBSTRING_INDEX(@string, ', ', 5);
---> RESULT: abc, def, ghi, jkl, mno
Était-ce utile?

La solution

You need some conditional logic:

select (case when @string = substring_index(@string, ', ', 3)
             then substring_index(@string, ', ', 3)
             else concat(substring_index(@string, ', ', 3), '...')
        end)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top