Retrieve 3 characters from a string, starting at position 1.
SELECT SUBSTRING(‘SQL Tutorial’, 1, 3) AS ExtractString; |
The SUBSTRING() function retrieves a specified number of characters from a string.
SUBSTRING(string, start, length) |
Parameter |
Description |
string |
Required: Specifies the string from which to extract characters. |
start |
Required: Specifies the starting position for extraction, where the first position in the string is numbered 1. |
length |
Required: Specifies the number of characters to extract from the string. This value must be positive. |
Works in: |
SQL Server (from 2008 onward), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse |
Retrieve 5 characters from the “CustomerName” column, starting at position 1.
SELECT SUBSTRING(CustomerName, 1, 5) AS ExtractString FROM Customers; |
Retrieve 100 characters from a string, starting at position 1.
SELECT SUBSTRING(‘SQL Tutorial’, 1, 100) AS ExtractString; |