Page 1 of 1

SELECT part of a field

Posted: Thu Nov 17, 2005 3:03 pm
by traffic
Hello...

I have a field in my database that is a 'text' field - post_text...

I want to be able to select just the first (500) characters in the field - but, can't seem to find any documentation for this...

Any help would be appreciated...


...

Posted: Thu Nov 17, 2005 3:11 pm
by yum-jelly
Have a look at MySQL String function... A little searching never hurt, :wink:


SUBSTRING()

Code: Select all

SELECT SUBSTRING(text_column_name, start_position, end_position) AS text FROM articles WHERE article_id = some_id;
Where as....

text_column_name = post_text <- your column name

start_position = where you want to start the string <- would be * 0 * if you want the first five hundred characters

end_position = where you want to end the string <- would be * 500 * if you want the first five hundred characters (number of character to grab [len])

yj

Re: SELECT part of a field

Posted: Thu Nov 17, 2005 3:22 pm
by RobertGonzalez
traffic wrote:Hello...

I have a field in my database that is a 'text' field - post_text...

I want to be able to select just the first (500) characters in the field - but, can't seem to find any documentation for this...

Any help would be appreciated...


...
Use the MySQL LEFT() function, like this...

Code: Select all

$sql = "SELECT LEFT('fieldname', 500) FROM table";

Posted: Thu Nov 17, 2005 5:09 pm
by Grim...
If you want to do it with PHP (this is the PHP forum, after all) get the field as normal, then use

Code: Select all

$post_text = substr($post_text, 0, 500);
However, using SQL will be a tiny bit faster, and is the better way to do it.