PDA

View Full Version : Code to pull only up to a certain amount of chars..


cyphix
02-01-2005, 11:31 AM
I'm certain there is a function to do this but I forget what it is...

Say I am pulling values from the database, but I only want to pull like the first 10 chars of the value.. how do I do that?

Thanks!

Ökii
02-01-2005, 12:12 PM
SUBSTRING(str,pos)
SUBSTRING(str FROM pos)
SUBSTRING(str,pos,len)
SUBSTRING(str FROM pos FOR len)
The forms without a len argument return a substring from string str starting at position pos. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are SQL-92 syntax.
mysql> SELECT SUBSTRING('Quadratically',5);
-> 'ratically'
mysql> SELECT SUBSTRING('foobarbar' FROM 4);
-> 'barbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);
-> 'ratica'

This function is multi-byte safe.

from the manual, so

'SELECT (`field` FROM 0 FOR 10) AS `fld` FROM `table` WHERE 1';

should suffice.

You might also check SUBSTRING_INDEX

cyphix
02-01-2005, 12:49 PM
Thanks Ökii! :thumbsup: