SQL Server SUBSTRING() Function
Last Updated :
17 Jun, 2024
The SQL Server SUBSTRING function extracts a substring from a string, starting at a specified position and with an optional length.
The SUBSTRING function also works in Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse.
Syntax
The SQL SUBSTRING function syntax is:
SUBSTRING(input_string, start, length);
Parameters
- Input_string: It can be a character, binary, text, or image expression.
- Start: An integer defines the location where the returned substring starts. The first position in the string is 1.
- Length: A positive integer specifies the number of characters to be returned from the substring.
- Returns: It returns a substring with a specified length starting from a location in an input string.
Rules for Using SUBSTRING() Function in SQL
- All three arguments are required in the SQL substring() function. If the starting position exceeds the maximum number of characters in the expression, the SQL Server substring() function returns nothing.
- The total length can exceed the maximum character length of the original string.
- In this case, the resulting substring is the entire string from the expression start position to the expression end character.
SQL Server SUBSTRING() Function Examples
Let's look at some examples of the SUBSTRING() function in SQL and understand how to use it in SQL Server.
In this example, we use the SUBSTRING function in SQL Server.
Query:
SELECT SUBSTRING('GeeksforGeeks',1,5);
Output:
.png)
SUBSTRING() Function with Literal Strings Example
Consider this SQL SUBSTRING function command:
SELECT SUBSTRING ('SQL In Geeksforgeeks', 7, 18) AS ExtractString
It will take the original string 'SQL In Geeksforgeeks' and extract a substring beginning with the 7th character and extracting a length of 18 characters. A new column with the alias ExtractString will be returned along with the resulting substring.
Output

SUBSTRING() Function With Table Columns Example
Let's create a table and use SUBSTRING() function on table column.
Query :
CREATE TABLE Player_Details (
PlayerId INT PRIMARY KEY,
PlayerName VARCHAR(50),
City VARCHAR(50)
);
INSERT INTO Player_Details (PlayerId, PlayerName, City)
VALUES
(1,'John', 'New York'),
(2,'Sarah', 'Los Angeles'),
(3,'David', 'Chicago'),
(4,'Emily', 'Houston'),
(5,'Michael', 'Phoenix'),
(6,'Ava', 'Philadelphia'),
(7,'Joshua', 'San Antonio'),
(8,'Sophia', 'San Diego'),
(9,'Daniel', 'Dallas'),
(10,'Olivia', 'San Jose');
Output:
.png)
In this example, we will use the SUBSTRING() function on a table column.
Query
SELECT SUBSTRING(PlayerName, 1, 3) AS ExtractString
FROM Player_Details;
Output
.png)
The PlayerName column in the subquery receives the SUBSTRING function, which chooses the first three characters of each name. The outcome of this substring operation is to return the PlayerName column from the subquery in a new column with the alias ExtractString.
Using SUBSTRING on a Nested Queries
Assuming you want to use the SUBSTRING function on a nested query within the player_Details table, you could use the following SQL code
Query
SELECT SUBSTRING(subquery.PlayerName, 1, 3) AS ShortenedName, subquery.City
FROM (
SELECT *
FROM Player_Details
WHERE City = 'New York'
) AS subquery;
Output

Important Function About SQL SUBSTRING Function
- The SUBSTRING() function extracts a substring from a string, starting at a specified position and with an optional length.
- It can be used with literal strings or columns in a table.
- The LEFT() and RIGHT() functions are also implementation of SUBSTRING() Function.
- Using SUBSTRING() in the WHERE clause negatively impacts the query performance, as the function will be executed for each row.
Similar Reads
SQL Server TRIM() Function The SQL Server TRIM() function omits the space character or additional stated characters from the beginning or the end of a specified string. TRIM in SQL ServerThe TRIM function in SQL Server is used to remove leading and trailing spaces from a string. Itâs particularly useful for manipulating and c
2 min read
STR() Function in SQL Server The STR() function converts a numeric value to a character value. Syntax : STR(float_expression [, length [, decimal]]) Parameter : This method accepts three parameters as mentioned above and described below : float_expression : It is a numeric expression that evaluates to an approximate number with
1 min read
SUBSTRING() function in MySQL SUBSTRING() : function in MySQL is used to derive substring from any given string .It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string. Syntax : SUBSTRING(string, start, length) OR SUB
2 min read
SQL | String functions SQL String Functions are powerful tools that allow us to manipulate, format, and extract specific parts of text data in our database. These functions are essential for tasks like cleaning up data, comparing strings, and combining text fields. Whether we're working with names, addresses, or any form
7 min read
SQL Server | STUFF() Function The STUFF() function in SQL Server is a powerful string manipulation tool used to delete a specified length of characters from a string and insert another set of characters at a given starting position. This function becomes particularly useful in scenarios where complex string operations are requir
2 min read