Domanda

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
È stato utile?

Soluzione

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top