Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

truncating text 2

Status
Not open for further replies.

shamrox

Programmer
Sep 27, 2001
81
US
Ok, I'm blanking here. What is the code that shortens a line of text from a database record if it's to long. example:
record is "This is what the text actually is."
output to screen would be "This is what..."


so I tell it I only want the first 20 characters or so...I know it's simple, I just can't recall it.

Thanks.

 
select id,CONCAT(SUBSTRING(long_text_col,1,YY),'...') from table where col='foo'

where YY = lenght in chars.

example:

field long_text is: "This is what the text actually is."

select id,CONCAT(SUBSTRING(long_text_col,1,11),'...') from table where col='foo'

result: "This is what..."

Take a look to:
 
However, you will end up with something like this when the text is short:
This is short....

It might be advantageous to add the ellipsis in PHP after retrieving the data from the database. That way you can make clear cases for short/exact/long text.
 
you could check the length of the string and truncate only when needed.
Code:
select if(length(long_text_col) > 10,CONCAT(SUBSTRING(long_text_col,1,11),'...'),CONCAT(long_text_col,'...'));

basically: if its longer than 10 character, chop and add ... otherwise just add ...

clear ? :)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
clarify a little more..

Code:
select if(length(long_text_col) > 10,CONCAT(SUBSTRING(long_text_col,1,11),'...'),CONCAT(long_text_col,'...')) FROM table WHERE stuff etc ... ;


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top