Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit length of longtext field in SELECT results

I'm executing a SELECT query on a table in MySQL using the command-line interface (not a GUI client):

SELECT * FROM blog_entry;

One of blog_entry's fields is of type 'longtext' and is such a long piece of text that when the result is displayed in my terminal the display of rows takes more than one line. This causes an ugly mess of a display, where columns aren't easily visible. What technique can I use in my SELECT query that would limit the number of characters displayed for each field so that the printed row results don't overflow to new lines?

like image 340
maxm Avatar asked Feb 11 '12 23:02

maxm


2 Answers

Use MySQL's SUBSTRING function, as described in the documentation. Like:

SELECT SUBSTRING(`text`, 1, 100) FROM blog_entry; 

To select first 100 chars.

like image 58
Oldskool Avatar answered Sep 18 '22 05:09

Oldskool


You can use the LEFT() function to get only the first characters:

SELECT LEFT(LongField, 20) AS LongField_First20chars FROM ... 
like image 41
ypercubeᵀᴹ Avatar answered Sep 21 '22 05:09

ypercubeᵀᴹ