SUBSTRING (Transact-SQL)

Extracts a specified number of characters from a string.

Syntax

SELECT SUBSTRING ( , , )

This function returns a specified number of characters from a string, starting at a specified location.

Parameters

Return Value

Returns the specified substring. If or are invalid, the function returns NULL.

Examples

Example 1: Extracting a string from a name

SELECT SUBSTRING('John Doe', 1, 4);

Returns: 'John'

Example 2: Extracting a string from a full name

SELECT SUBSTRING('John Doe', 5, 2);

Returns: 'Doe'

Example 3: Extracting from a numeric string

SELECT SUBSTRING('12345', 2, 3);

Returns: '345'