A substring function extracts a portion of a string based on a starting position and, depending on the database system, a specified length. Syntax can vary between SQL platforms.
Business identifiers often contain multiple pieces of information inside one text field. Extracting part of a string can help analysts prepare and categorize data.
If a customer code contains a region prefix, a substring operation can extract the prefix for regional analysis.
SELECT
SUBSTRING(CustomerCode, 1, 3) AS RegionCode
FROM Customers;
Substring operations are useful for extracting codes, prefixes, suffixes, product identifiers, account references, and parts of addresses.
- SUBSTRING extracts part of a string.
- A starting position is commonly required.
- A length may also be specified.
- Syntax differs between database systems.
- It is useful for parsing structured text.
What does SUBSTRING do?
SUBSTRING extracts a portion of a text value.
Why use SUBSTRING?
It can extract meaningful portions such as prefixes, suffixes, or codes from a larger text value.