SUBSTRING()

The SUBSTRING function returns a portion of a specified character, binary, text, or image expression.

Signature

SUBSTRING ( expression , start , length )

Parameters

  • expression — A character, binary, text, ntext, or image expression.
  • start — An integer or bigint expression that specifies where the returned characters start. (The numbering is 1 based, meaning that the first character in the expression is 1). If *start* is less than 1, the returned expression begins at the first character that is specified in *expression*. In this case, the number of characters that are returned is the largest value of either the sum of *start* + *length* - 1, or 0. If *start* is greater than the number of characters in the value expression, a zero-length expression is returned.
  • length — A positive integer or bigint expression that specifies how many characters of the *expression* are returned. If *length* is negative, an error is generated and the statement is terminated. If the sum of *start* and *length* is greater than the number of characters in *expression*, the whole value expression beginning at *start* is returned. If *length* is omitted, all characters from the start position to the end of the expression is returned.

Examples

SELECT SUBSTRING('keyboard', 4, 5) AS part;
part
-----
board
SELECT name,
       SUBSTRING(name, 1, 1) AS Initial,
       SUBSTRING(name, 3, 2) AS ThirdAndFourthCharacters
FROM sys.databases
WHERE database_id < 5;
master|m|st
tempdb|t|mp
model|m|de
msdb|m|db
SELECT SUBSTRING('abcdef', 2, 3) AS x;
bcd

Run against SQL Server 16.0.4265.3 on 2026-09-13. This is the output we got, not the documentation’s.

Available on

  • SQL Server 2017 and later
  • Azure SQL Database
  • Azure SQL Managed Instance
  • Azure Synapse Analytics
  • Analytics Platform System (PDW)
  • Microsoft Fabric warehouse
  • Fabric SQL database

Signature and description adapted from the Microsoft SQL documentation, used under the CC BY 4.0. SQL Server and Azure are trademarks of the Microsoft group of companies. This page is not affiliated with Microsoft.