String functions
String functions manipulate text values in SQL — concatenating, trimming, searching, splitting, and reformatting strings as part of a query.
Overview
String functions are built-in SQL functions for working with VARCHAR/text data: measuring length, changing case, searching and replacing substrings, trimming whitespace, splitting and joining, and formatting output. They show up constantly in data cleaning, parsing semi-structured text, and building human-readable reports.
Common functions
Copy code
SELECT
LENGTH(email) AS email_length,
LOWER(email) AS email_lower,
TRIM(name) AS trimmed_name,
email || ' <verified>' AS annotated_email, -- concatenation
SPLIT_PART(email, '@', 2) AS domain,
REPLACE(phone, '-', '') AS phone_digits_only,
SUBSTRING(sku, 1, 3) AS sku_prefix,
STARTS_WITH(url, 'https://') AS is_https
FROM customers;
Common categories: case conversion (UPPER/LOWER/INITCAP), trimming (TRIM/LTRIM/RTRIM), search (POSITION, CONTAINS, LIKE), extraction (SUBSTRING, SPLIT_PART, LEFT/RIGHT), and aggregation (STRING_AGG/GROUP_CONCAT to join many rows' strings into one).
Copy code
SELECT department, STRING_AGG(employee_name, ', ' ORDER BY employee_name) AS team
FROM employees
GROUP BY department;
DuckDB specifics
DuckDB implements a large, Postgres-compatible string function library plus several conveniences: printf()/format() for template-style formatting, list_string_agg semantics via STRING_AGG, CONTAINS/STARTS_WITH/ENDS_WITH as readable alternatives to LIKE patterns, and native regular-expression functions (regexp_matches, regexp_replace, regexp_extract, regexp_extract_all). DuckDB also supports the || concatenation operator across strings, lists, and even structs in some contexts, and string_split/str_split to turn a delimited string directly into a LIST:
Copy code
SELECT string_split('a,b,c', ',') AS parts;
-- ['a', 'b', 'c']
Related terms
SQL engines support regular expressions through functions and operators like REGEXP_MATCHES, REGEXP_REPLACE, and SIMILAR TO for pattern-based text matching and extraction.
SQL function →A SQL function is a reusable piece of code that performs a specific operation and returns a result.
aggregate functions →Aggregate functions are SQL operations that perform calculations across multiple rows of data to produce a single summary value.
LIKE operator →The LIKE operator performs pattern matching on string values, using % as a wildcard for any number of characters and _ for a single character.
window functions →Window functions allow you to perform calculations across a set of table rows that are somehow related to the current row, similar to aggregate functions,…
Data cleansing →Data cleansing (or data cleaning) is the process of detecting and correcting inaccurate, incomplete, inconsistent, or malformed data so it's reliable for analysis and downstream systems.
FAQS
Use an aggregate string function such as STRING_AGG(column, separator ORDER BY ...), which DuckDB and most modern SQL engines support, to join values from many rows into a single delimited string per group.
LIKE uses wildcard pattern matching (% and _), while CONTAINS(), STARTS_WITH(), and ENDS_WITH() are plain substring-check functions with fixed text, which are often clearer and avoid the need to escape wildcard characters.
