SQL SUBSTRING extracts a portion of a string starting at a 1-based position for a given length. Use the syntax SUBSTRING(source, start, length) or vendor variations like SUBSTR. It solves tasks such as trimming prefixes, parsing codes, and isolating date parts across MySQL, PostgreSQL, SQL Server, and SQLite.
SQL SUBSTRING extracts a segment of text from a string, starting at a specified 1-based index for a defined length. It works in every major RDBMS, sometimes named SUBSTR.
SQL SUBSTRING returns a VARCHAR, CHAR, or TEXT value containing the requested slice of the original string. The data type depends on the source column and RDBMS casting rules.
Use SUBSTRING(source_expression FROM start FOR length) in PostgreSQL or SUBSTRING(source, start, length) in SQL Server, MySQL, and SQLite. Both forms need a 1-based start position and an integer length.
SQL standards count characters beginning with 1 to align with human counting. A start of 1 means “begin at the first character.” Supplying 0 or a negative value typically raises an error or returns an empty string.
Most engines truncate the result silently, returning up to the end of the string. You never get an error for overshooting length, so always validate assumptions when parsing user data.
Call SUBSTRING(column, 1, n) where n is the number of characters you need. This approach quickly pulls ISO country codes, file name prefixes, or SKU roots.
Use SUBSTRING(column, CHAR_LENGTH(column) - n + 1, n). PostgreSQL and SQL Server also support RIGHT(column, n) as a shorthand.
Yes. Pass dynamic values for start and length—perhaps derived with CHARINDEX, POSITION, or REGEXP functions—to slice data based on runtime patterns.
Find the delimiter position using POSITION('/' IN path) or CHARINDEX('/', path), add 1, and feed that to SUBSTRING. The result isolates tokens between delimiters.
Choose SUBSTRING for fixed-position slices or simple delimiter parsing. Pick regular expressions for complex patterns like optional groups or varying token sizes.
Modern engines treat VARCHAR/TEXT as Unicode by default. SUBSTRING counts characters, not bytes, so multilingual text stays intact. Always store text in UTF-8 or UTF-16 capable columns.
Yes. Oracle, SQLite, and older MySQL versions expose SUBSTR, but parameters mirror SUBSTRING. Use whichever your vendor documents.
Embed SUBSTRING(column, start, length) within predicates to filter rows. Because this prevents index usage, consider storing a computed column for large tables.
Common tasks include parsing dates from text logs, extracting domain names, masking PII like credit card numbers, and deriving month codes for dimensional modeling.
SUBSTRING is CPU-cheap but can block index seeks on the column. Compute values in a view or materialized column when used heavily in joins or filters.
Validate source lengths, handle NULLs with COALESCE, document assumptions, and encapsulate complex substrings in expressive views or CTEs for clarity.
SQL SUBSTRING slices strings with simple 1-based start and length parameters. Combine it with POSITION, LENGTH, and pattern searches for flexible text wrangling. Validate inputs, watch indexing, and you’ll master string extraction across every RDBMS.
Yes. The ANSI SQL syntax is SUBSTRING(source FROM start FOR length). Vendor functions differ slightly but conceptually match.
PostgreSQL allows SUBSTRING(source FROM start) to return until the end. In MySQL and SQL Server, pass a large number or chain RIGHT/LEN logic.
Most engines restrict SUBSTRING to text types. Cast binary data to HEX or Base64 string first, or use vendor-specific binary slice functions.
LEFT/RIGHT focus on fixed slices at string ends and can be clearer. SUBSTRING is more flexible and works everywhere.