The SUBSTRING function extracts a portion of a string based on a starting position and, depending on the database, a specified length.
Many business fields contain useful information embedded inside larger strings. SUBSTRING helps extract that information.
Extract the first three characters of a customer code.
SELECT
CustomerCode,
SUBSTRING(CustomerCode, 1, 3) AS RegionCode
FROM Customers;
SUBSTRING is useful for parsing identifiers, extracting codes, preparing reporting fields, and cleaning imported data.
- SUBSTRING extracts part of a string.
- A starting position is used.
- A length may also be specified.
- Position numbering can vary by database.
- It is useful for parsing text.
What does SUBSTRING do?
SUBSTRING extracts a specified portion of a text value.
Where is SUBSTRING useful?
It is useful for extracting codes, prefixes, identifiers, and other pieces of information from text.