Substring function
The string functionSUBSTRING
has the role of extracting and returning characters from an expression.
The type of expression can be: character, binary, text, ntext, or image.
Basic Substring Example
SELECT SUBSTRING('SQL course', 5, 6);
Result: course
States table:
STATE_ID | NAME |
---|---|
1 | Arizona |
2 | California |
3 | Texas |
4 | Michigan |
Substring Example:
SELECT Name, SUBSTRING(name, 1, 1) AS Initial, SUBSTRING(name, 1, 3) AS Short_name
FROM states;
Substring Result:
Name | Initial | Short_name |
---|---|---|
Arizona | A | Ari |
California | C | Cal |
Texas | T | Tex |
Michigan | M | Mic |
See also: T-SQL Functions -> Patindex | Replace | Right | Rtrim | Upper