MySQL truncate text with ellipsis

倖福魔咒の 提交于 2019-12-01 02:45:41
select case when length(message) > 7 
then concat(substring(message, 1, 7), '...')
else message end as adapted_message
from ...

to test/confirm:

SELECT CASE WHEN LENGTH('1234567890') > 7 
THEN CONCAT(SUBSTRING('1234567890', 1, 7), '...') 
ELSE '1234567890' END AS adapted_message

UNION  

SELECT CASE WHEN LENGTH('12345') > 7 
THEN CONCAT(SUBSTRING('12345', 1, 7), '...') 
ELSE '12345' END AS adapted_message

or...

SELECT CONCAT(LEFT(message, 7), IF(LENGTH(message)>7, "…", ""))
FROM table
Massivo

Here's a simple one line solution:

IF(CHAR_LENGTH(message) > 10, CONCAT(LEFT(message, 7),"..."), message)

Have a look at the MySQL string functions, documented here. You should be able to use some combination of substring and concat to achieve your desired behaviour.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!