T-SQL Tutorial

T-SQL Substring function


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_IDNAME
1Arizona
2California
3Texas
4Michigan

Substring Example:

SELECT Name, SUBSTRING(name, 1, 1) AS Initial, SUBSTRING(name, 1, 3) AS Short_name
FROM states;


Substring Result:

NameInitialShort_name
ArizonaAAri
CaliforniaCCal
TexasTTex
MichiganMMic

See also: T-SQL Functions -> Patindex | Replace | Right | Rtrim | Upper