Limit number of characters in Mysql PHP. add a ... if the length is longer.

Dear Experts.

How do I limit the number of character in the mysql query, but add a ... if the text is longer than say 20 characters. If it is shorter than 20 character then the ... at the end would not show.

This is what I have so far, it limit the number of character but I do not know how to add the ... at the end.

SELECT LEFT(Help.Name, 20) FROM Help WHERE Help.HelpID = 1

If this is not possible in the mysql query would it be possible in the php echo statement.

e.g the result I am looking if the result of the Help.Name was

to be or not to be that is the question

then the result I am looking for it

to be or not to be t...

but if the query result was less than 20 character a ... would not be added to the end.

Many thanks for your help


LightwalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

theGhost_k8Database ConsultantCommented:
you can use combination of if, length & concat
SELECT IF(LENGTH(COLUMN)>20,CONCAT(COLUMN,'...'),COLUMN) FROM TABLE;
0
theGhost_k8Database ConsultantCommented:
Oops.. Use this:
Select IF(LENGTH(Help.Name)>20,CONCAT(LEFT(Help.Name,20),'..') FROM Help WHERE Help.HelpID = 1
0
LightwalkerAuthor Commented:
Hi theGhost k8,

Thanks, for some reason your example is giving me a syntax error, but I can not find it.
0
theGhost_k8Database ConsultantCommented:
Select IF(LENGTH(Help.Name)>20,CONCAT(LEFT(Help.Name,20),'..'),Help.Name) FROM Help WHERE Help.HelpID = 1;

0
XzKtoCommented:
"theGhost_k8" got it right, but a little better variant, imho:

SELECT IF(LENGTH(Help.Name)>20,INSERT(Help.Name,21,-1,'...'),Help.Name) AS Name FROM Help WHERE Help.HelpID = 1;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.