SUBSTRING

The SUBSTRING function returns a portion of a character or binary string.

The SUBSTRING function has the following syntax:

SUBSTRING(expression, start, length)

SUBSTRING always takes three parameters. The first can be a character or binary string, a column name, or a string-valued expression that includes a column name. The second parameter specifies the position at which the substring should begin. The third specifies the length (in number of characters) of the string to be returned.

This example displays the last name and first initial of each author ¾ for example, Bennet A:

SELECT au_lname, SUBSTRING(au_fname, 1, 1)
FROM authors

Here's how to display the second, third, and fourth characters of the string constant abcdef:

SELECT x = SUBSTRING('abcdef', 2, 3)

x
----------
bcd

(1 row(s) affected)