Docs
Docs/Advanced SQL/String Functions

String Functions

Manipulate text data in queries

String functions help you search, transform, and format text data.

Common String Functions

FunctionExampleResult
UPPER()UPPER('hello')'HELLO'
LOWER()LOWER('HELLO')'hello'
LENGTH()LENGTH('hello')5
TRIM()TRIM(' hi ')'hi'
SUBSTR()SUBSTR('hello', 1, 3)'hel'
REPLACE()REPLACE('hello', 'l', 'L')'heLLo'

String Concatenation

-- SQLite / PostgreSQL
SELECT first_name || ' ' || last_name AS full_name FROM users;

-- MySQL
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;

-- Handle NULL (PostgreSQL)
SELECT CONCAT_WS(' ', first_name, middle_name, last_name) FROM users;

Practical Examples

-- Clean up data
SELECT TRIM(LOWER(email)) AS clean_email FROM users;

-- Extract domain from email
SELECT SUBSTR(email, INSTR(email, '@') + 1) AS domain FROM users;

-- Format phone number
SELECT 
    '(' || SUBSTR(phone, 1, 3) || ') ' || 
    SUBSTR(phone, 4, 3) || '-' || 
    SUBSTR(phone, 7, 4) AS formatted_phone
FROM users;

Need help?

Join our Discord community for support and discussions.

Join Discord