THE WORLD'S LARGEST WEB DEVELOPER SITE

SQL Server SUBSTRING() Function

❮ SQL Server Functions

Example

Extract a substring from a string:

SELECT SUBSTRING('SQL Tutorial', 1, 3) AS ExtractString;
Try it Yourself »

Definition and Usage

The SUBSTRING() function extracts a substring from a string.

Syntax

SUBSTRING(string, start_pos, number_of_chars)

Parameter Values

Parameter Description
string Required. The string to extract from
start_pos Required. The position to start extraction from. The first position in string is 1
number_of_chars Required. The number of characters to extract

Note

  • If number_of_chars is a negative number, the SUBSTRING() function will return an error

Technical Details

Works in: SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

More Examples

Example

Extract a substring from the text in a column:

SELECT SUBSTRING(CustomerName, 1, 5) AS ExtractString
FROM Customers;
Try it Yourself »

Example

Extract a substring from a string:

SELECT SUBSTRING('SQL Tutorial', 1, 100) AS ExtractString;
Try it Yourself »

❮ SQL Server Functions