In SQL, Substring function is used to extract a portion(substring) of a specified string value based on specified starting and ending length.
In SQL, A string is a data type used to store data that can contain combination of numbers, letters, whitespaces and symbols. In simple term, String is a word or a sequence of characters.
A substring is a consecutive sequence of characters or a portion of a string.
SUBSTRING(string, starting_position, length)
• Specify the 'string' in single quotes ( ' ' ) or 'column' from which you want to extract the substring.
• Specify the 'starting position' from where you want to begin substring extraction.
• Specify the 'length' till what you want to extract the substring.
1. Let's assume we have a table named "[Employees]".
2. Let's assume you want to extract the first three characters from Employee_Name column.
3. Run below SQL statement :
SELECT SUBSTRING(Employee_Name, 1, 3) FROM [Employees];
4. In above statement, SUBSTRING function extracts initial three characters from Employee_Name column.
Using this function in a select statement won't modify the [Employees] table directly, but it will only be reflected in the select statement's output.
The substring function in SQL extracts a portion of a string based on specified starting position and length.
Substring function is used to extract a portion of a string in SQL.
To get the last 3 characters in a string in SQL, use the substring function with the appropriate parameters.
To get the first 5 characters of a string in SQL, utilize the substring function with the starting position set to 1 and the length set to 5.