Database Abstraction Layer for PHP

User Tools

Site Tools



From Version 5.21.0-beta-2

string substr(
    string $fieldName,
    int $startPoint,
    optional int $length


The method substr() creates an SQL statement that returns the substring of a specified string field.



The fieldname to be substringed. The fieldname must be accessible as part of the current SQL statement.


The starting point within the original string for the substring to begin. Offsets in SQL statements are 1 based, rather than the normal 0 (Zero) based offsets used in PHP.


If specified, the length of the string to return. If not specified, then the remainder of the string starting at the start point is returned


* To return the an associative array of codes plus 
* the first 20 characters of a description.
$sql = "SELECT code, {$db->substr('description',1,20)} FROM some_table";
$data = $db->getAssoc($sql); 

Negative Offsets

Not all databases support the use of negative offsets (to return values based on the end of the string), so use in portable applications is not recommended.

v5/reference/connection/substr.txt · Last modified: 2016/04/10 23:42 by mnewnham