Using SUBSTRING

Accessing and Changing Relational Data

Accessing and Changing Relational Data

Using SUBSTRING

The SUBSTRING function returns a portion of either a character or binary string, or a text string, and takes three parameters:

  • A character or binary string, a column name, or a string-valued expression that includes a column name.

  • The position at which the substring should begin.

  • The length (in number of characters, or in number of bytes for binary) of the string to be returned.

This example displays the first initial and last name of each employee, for example, A Fuller:

USE Northwind
SELECT SUBSTRING(FirstName, 1, 1), LastName
FROM Employees

This example displays the second, third, and fourth characters of the string constant abcdef:

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

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

(1 row(s) affected)

See Also

String Functions