Returns the string formed by extracting the specified number of characters from another string.
substring(expression, start, length)
is a binary or character column name, variable, or constant expression. Can be char, nchar, unichar, varchar, univarchar, or nvarchar data, binary, or varbinary.
specifies the character position at which the substring begins.
specifies the number of characters in the substring.
Displays the last name and first initial of each author, for example, “Bennet A.”:
select au_lname, substring(au_fname, 1, 1) from authors
Converts the author’s last name to uppercase, then displays the first three characters:
select substring(upper(au_lname), 1, 3) from authors
Concatenates pub_id and title_id, then displays the first six characters of the resulting string:
select substring((pub_id + title_id), 1, 6) from titles
Extracts the lower four digits from a binary field, where each position represents two binary digits:
select substring(xactid,5,2) from syslogs
substring, a string function, returns part of a character or binary string. For general information about string functions, see Transact-SQL Users Guide.
If substring’s second argument is NULL, the result is NULL. If substring’s first or third argument is NULL, the result is blank..
If the start position from the beginning of uchar_expr1 falls in the middle of a surrogate pair, start is adjusted to one less. If the start length position from the beginning of uchar_expr1 falls in the middle of a surrogate pair, length is adjusted to one less.
ANSI SQL – Compliance level: Transact-SQL extension.
Any user can execute substring.