Al Bundy wrote:
> Is there posibility in SQL to cut strings that are longer than some $length_variable and add '...' at
> the end of new string but only when the string was cutted - if not we leave it...
Simple version (cuts anywhere):
SET @pivot := 25;
SELECT
CASE
WHEN LENGTH(bar) > @pivot THEN CONCAT(SUBSTRING(bar, 1, @pivot), '...')
ELSE bar
END AS 'Text'
FROM foo;
This one only cuts at space characters:
SET @pivot := 25;
SELECT
CASE WHEN LENGTH(bar) > @pivot THEN
CONCAT(
SUBSTRING_INDEX(
SUBSTRING(bar, 1, @pivot), ' ',
LENGTH(SUBSTRING(bar, 1, @pivot)) - LENGTH(REPLACE(SUBSTRING(bar, 1, @pivot), ' ', '')))
, '...')
ELSE bar
END AS 'Text'
FROM foo;
--
felix
Please use
BBCode to format your messages in this forum.