Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Master SQL string manipulation across PostgreSQL, MySQL, SQL Server, and BigQuery. Compare SUBSTRING, STRING_SPLIT, and regular expression extraction.
Quick answer
SQL string functions manipulate textual data across databases, but function names, indexing rules, and splitting mechanics vary significantly. PostgreSQL uses SUBSTRING() with 1-based indexing and regex matching, SQL Server uses SUBSTRING() and STRING_SPLIT(), MySQL uses SUBSTR() and REGEXP_SUBSTR(), and BigQuery provides SPLIT() into native ARRAY types.
FAQs
Does SUBSTRING in SQL use 0-based or 1-based indexing?
All major SQL dialects use 1-based indexing. The first character in a string is at position 1.
Why does SQL Server SUBSTRING fail with two arguments?
Unlike PostgreSQL and MySQL, SQL Server requires all three arguments (string, start, length). You must provide LEN(string) as the third argument to read to the end.
How do I split a comma-separated string in SQLite?
SQLite lacks a built-in split function. You must use recursive Common Table Expressions (CTEs) or handle string splitting in application code.
What is the difference between LIKE and REGEXP in SQL?
LIKE supports only simple wildcards (% and _), while REGEXP supports full POSIX or PCRE regular expression patterns, character classes, and quantifiers.
Build fast text search directly in PostgreSQL without external search engines. Master to_tsvector, to_tsquery, relevance ranking with ts_rank, and GIN indexes.
Data cleaning, pipeline ingestion, and analytics require extensive string manipulation in SQL: extracting substrings, parsing email domains, splitting comma-delimited tokens, and matching regex patterns. However, string functions are among the least standardized areas of SQL across major database engines.
Writing portable SQL or migrating between database engines frequently breaks when functions like SUBSTRING or SPLIT behave differently. Understanding the syntactic nuances between PostgreSQL, MySQL, SQL Server, and BigQuery ensures your text processing queries remain accurate and performant.
1-based indexing: the universal SQL convention
Unlike programming languages like Python, JavaScript, and C that use 0-based indexing for strings and arrays, SQL standards specify 1-based indexing. In all major database dialects, the first character of a string is at position 1.
Passing 0 as the starting index in SQL Server or PostgreSQL does not throw an error; instead, the engine treats 0 as an offset before the first character. For example, SUBSTRING('Database', 0, 3) in SQL Server starts before character 1 and returns only 2 characters ('Da'), causing subtle extraction bugs.
Extracting substrings: SUBSTRING vs SUBSTR
Different database engines support slightly different function signatures for basic substring extraction. Review our cross-database guide on SQL string concatenation across databases for related syntax patterns.
Substring syntax across database engines
-- PostgreSQL (supports ANSI standard and 2-parameter shorthand):SELECT SUBSTRING('user_session_token' FROM 1 FOR 4); -- Returns 'user'SELECT SUBSTRING('user_session_token', 6); -- Returns 'session_token'-- MySQL and SQLite (supports SUBSTRING and SUBSTR synonym):SELECT SUBSTR('user_session_token', 1, 4); -- Returns 'user'SELECT SUBSTRING('user_session_token', 6); -- Returns 'session_token'-- SQL Server (requires all 3 arguments; no 2-argument shorthand!):SELECT SUBSTRING('user_session_token', 1, 4); -- Returns 'user'-- SUBSTRING('token', 6) -> Syntax Error! Must pass length:SELECT SUBSTRING('user_session_token', 6, LEN('user_session_token'));-- BigQuery (supports SUBSTR):SELECT SUBSTR('user_session_token', 1, 4); -- Returns 'user'
A frequent task is breaking a comma-separated or pipe-separated string into individual rows. Different dialects approach this with wildly different mechanisms:
SQL Server: STRING_SPLIT() table-valued function
SQL Server provides STRING_SPLIT(), which returns a table with a column named value. In SQL Server 2022 and later, an optional third parameter preserves the original ordinal position of each element.
SQL Server STRING_SPLIT example
-- SQL Server 2022+ syntax preserving ordinal indexSELECT value AS tag_name, ordinalFROM STRING_SPLIT('sql,postgres,mysql,bigquery', ',', 1);
According to Microsoft Learn STRING_SPLIT documentation, STRING_SPLIT operates as an inline table-valued function that can be combined with CROSS APPLY to normalize multi-value table columns.
PostgreSQL: string_to_array() and unnest()
PostgreSQL natively handles arrays. You convert a string to an array with string_to_array(), and expand it into rows using unnest().
PostgreSQL string splitting pattern
-- Split into array, then expand into rowsSELECT unnest(string_to_array('sql,postgres,mysql,bigquery', ',')) AS tag_name;-- Alternatively, use regexp_split_to_table directly:SELECT regexp_split_to_table('sql,postgres,mysql,bigquery', ',') AS tag_name;
BigQuery: SPLIT() into native ARRAY types
BigQuery treats arrays as first-class citizens. The SPLIT() function returns an ARRAY<STRING>, which you can query directly or unnest into rows with UNNEST().
BigQuery string split query
-- Returns an ARRAY<STRING>SELECT SPLIT('sql,postgres,mysql,bigquery', ',') AS tag_array;-- Flatten into distinct rows:SELECT tag_nameFROM UNNEST(SPLIT('sql,postgres,mysql,bigquery', ',')) AS tag_name;
Regular expression matching and extraction
When simple delimiters or fixed positions are insufficient, regular expressions extract patterns like email domains or ticket numbers.
Regex extraction across engines
-- PostgreSQL: SUBSTRING with POSIX regular expressionSELECT SUBSTRING('Contact support@example.com for help' FROM '@([A-Za-z0-9.-]+)');-- Returns 'example.com'-- MySQL 8.0+: REGEXP_SUBSTRSELECT REGEXP_SUBSTR('Invoice INV-2026-9481 generated', 'INV-[0-9]{4}-[0-9]+');-- Returns 'INV-2026-9481'-- BigQuery: REGEXP_EXTRACTSELECT REGEXP_EXTRACT('https://sqlfmt.app/blog/page-1', r'/blog/([^/?]+)');-- Returns 'page-1'
Actionable checklist for SQL string processing
Remember 1-based indexing: verify character offsets start at 1, not 0, across all queries.
Guard length arguments: always supply explicit length arguments in SQL Server SUBSTRING calls.
Avoid row-level regex in WHERE filters: avoid REGEXP in WHERE clauses on unindexed columns to prevent table scans.
Preserve split order: use ordinal flags in SQL Server 2022+ or WITH ORDINALITY in PostgreSQL when order matters.
Trim whitespace: wrap split values in TRIM() to remove leading and trailing spaces from user input.
Test NULL handling: verify whether input NULLs return NULL or raise errors in your specific database dialect.
Wrapping up
String processing in SQL requires careful attention to dialect differences, indexing offsets, and array capabilities. By mastering SUBSTRING, native string splitting, and regex extraction, you can build reliable data transformation pipelines that work reliably across any database engine. Format your complex string queries with sqlfmt or review our pricing options for automated SQL formatting.