SQL Server: SUBSTRING Function

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 allow 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 functions 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 functions examples and explore how to use the SUBSTRING functions in SQL Server (Transact-SQL).

For example:

SELECT SUBSTRING('adglob.in', 1, 4);
Result: 'adgl'

SELECT SUBSTRING('adglob.in', 5, 2);
Result: 'On'

SELECT SUBSTRING('adglob.in', 5, 1);
Result: 'o'

Leave a Reply