MySQL/SQL retrieve first 40 characters of a text field?
SELECT LEFT(field, 40) AS excerpt FROM table(s) WHERE ...
See the LEFT()
function.
As a rule of thumb, you should never do in PHP what MySQL can do for you. Think of it this way: You don't want to transmit anything more than strictly necessary from the DB to the requesting applications.
EDIT If you're going to use the entire data on the same page (i.e., with no intermediate request) more often than not, there's no reason not to fetch the full text at once. (See comments and Veger's answer.)
SELECT LEFT(MY_COLUMN, 40) FROM MY_TABLE
Function in the MySQL reference manual:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left
try this...
SELECT LEFT(field name, 40) FROM table name WHERE condition for first 40 and
SELECT RIGHT(field name, 40) FROM table name WHERE condition for last 40