Skip navigation links

MySQL Forums :: Newbie :: Cutting Strings


Advanced Search

Re: Cutting Strings
Posted by: Felix Geerinckx ()
Date: December 20, 2005 10:37AM

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.

Options: ReplyQuote


Subject Written By Posted
Cutting Strings Al Bundy 12/20/2005 08:06AM
Re: Cutting Strings Felix Geerinckx 12/20/2005 10:37AM
Re: Cutting Strings Al Bundy 12/21/2005 08:21AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.