Just the first 100 characters of a query
Moderator: General Moderators
Just the first 100 characters of a query
So I have some text in a mysql database value, but I don't want to display all of it. What's the command to only return the first so many letters from it?
-
dyonak
- Forum Commoner
- Posts: 56
- Joined: Wed Jun 22, 2005 10:22 am
- Location: Minneapolis, MN
- Contact:
Would it be acceptible to retrieve the whole entry and then use PHP to only display the first 200 characters? If so something like this should work:
Code: Select all
$dbEntryTeaser = substr($dbEntry, 0, 100);
echo $dbEntryTeaser;
Last edited by dyonak on Thu Oct 06, 2005 4:23 pm, edited 1 time in total.
You CAN do it in your query
From the manual:
So if you want the first 100 characters, you could do:
From the manual:
Code: Select all
SUBSTRING(str,pos) , SUBSTRING(str FROM pos) , SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FOR len)
mysql> SELECT SUBSTRING('Quadratically',5);
-> 'ratically'
mysql> SELECT SUBSTRING('foobarbar' FROM 4);
-> 'barbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);
-> 'ratica'
mysql> SELECT SUBSTRING('Sakila', -3);
-> 'ila'
mysql> SELECT SUBSTRING('Sakila', -5, 3);
-> 'aki'
mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
-> 'ki'
SUBSTR() is a synonym for SUBSTRING(), added in MySQL 4.1.1.Code: Select all
SELECT
SUBSTRING(long_field_value,0,100)
FROM
myTableReal programmers don't comment their code. If it was hard to write, it should be hard to understand.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
SUBSTRING_INDEX() or
Useful Posts wrote:Chopping of text without losing words: chopping of text