SQL helper functions for common operations across all database dialects.
PDOdb provides 50+ SQL helper functions through the Db class. These helpers abstract dialect-specific SQL syntax, providing a unified API that works across MySQL, PostgreSQL, and SQLite.
String manipulation functions.
Topics covered:
upper(),lower()- Case conversiontrim(),ltrim(),rtrim()- Whitespace removalconcat()- String concatenationsubstring()- Extract substringlength()- String lengthreplace()- Replace substringreverse()- Reverse stringposition()- Find substring position
Mathematical operations and functions.
Topics covered:
abs()- Absolute valueceil(),floor(),round()- Roundingpower()- Exponentiationsqrt()- Square rootmod()- Modulo operationsign()- Sign of numberrandom()- Random numbers
Date and time functions.
Topics covered:
now(),curDate(),curTime()- Current date/timeyear(),month(),day()- Extract date partshour(),minute(),second()- Extract time partsdateAdd(),dateSub()- Date arithmeticdateDiff()- Difference between datesdateFormat()- Format datestimestamp()- Unix timestamps
NULL handling and coalescing.
Topics covered:
isNull(),isNotNull()- NULL checkscoalesce()- First non-NULL valueifNull()- NULL replacementnullIf()- Return NULL if equal- NULL-safe comparisons
Comparison operators and conditions.
Topics covered:
like(),ilike()- Pattern matchingnotLike()- Negated pattern matchingbetween(),notBetween()- Range checksin(),notIn()- Value listsnot()- Negation wrapper
Conditional logic and CASE statements.
Topics covered:
case()- CASE WHEN expressionsif()- Simple IF conditionsgreatest(),least()- Value comparison- Complex multi-condition logic
- Nested CASE statements
- Conditional aggregations
Boolean values and operations.
Topics covered:
true(),false()- Boolean literalsdefault()- DEFAULT keyword- Boolean expressions
- Dialect-specific boolean handling
- Boolean type conversions
Type casting and conversion functions.
Topics covered:
cast()- Type castingcastToInt(),castToString(),castToDate()- Specific castsgreatest(),least()- Type-aware comparisons- Type conversion across dialects
- Handling type differences
All helpers are accessed through the Db class:
use tommyknocker\pdodb\helpers\Db;
// In SELECT
$results = $db->find()
->from('users')
->select([
'name',
'email_upper' => Db::upper('email'),
'age_years' => Db::floor(Db::dateDiff('now', 'birthdate') / 365)
])
->get();
// In WHERE
$results = $db->find()
->from('products')
->where(Db::like('name', '%laptop%'))
->andWhere(Db::between('price', 100, 1000))
->get();
// In ORDER BY
$results = $db->find()
->from('users')
->orderBy(Db::lower('name'), 'ASC')
->get();php 01-string-helpers.phpPDODB_DRIVER=mysql php 01-string-helpers.phpPDODB_DRIVER=pgsql php 01-string-helpers.phpAll helper functions produce dialect-specific SQL:
| Function | MySQL | PostgreSQL | SQLite |
|---|---|---|---|
concat() |
CONCAT() |
|| operator |
|| operator |
ilike() |
LOWER() LIKE |
ILIKE |
LOWER() LIKE |
now() |
NOW() |
NOW() |
datetime('now') |
true() |
TRUE |
TRUE |
1 |
See Helper Functions Reference for complete documentation of all 50+ helper functions.
- JSON Helpers - JSON-specific operations
- Export Helpers - Data export functions
- Real-World Examples - Helper functions in context