SQL Server: SUBSTRING Function

This SQL Server tutorial explains how to use the SUBSTRING function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the SUBSTRING functions allows you to extract a substring from a string.

Syntax

The syntax for the SUBSTRING function in SQL Server (Transact-SQL) is:

SUBSTRING( string, start_position, length )

Parameters or Arguments

string
The source string to extract from.
start_position
The position to start extraction from. The first position in the string is always 1.
length
The number of characters to extract.

Note

  • The first position in string is 1.
  • If length is a negative number, then the SUBSTRING function will return an error.

Applies To

The SUBSTRING function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let’s look at some SQL Server SUBSTRING function examples and explore how to use the SUBSTRING function in SQL Server (Transact-SQL).

For example:

SELECT SUBSTRING('TechOnTheNet.com', 1, 4);
Result: 'Tech'

SELECT SUBSTRING('TechOnTheNet.com', 5, 2);
Result: 'On'

SELECT SUBSTRING('TechOnTheNet.com', 9, 1);
Result: 'e'