SUBSTRING() function enables us to retrieve a part of the string from the specified text Column value.
This can be used on a Phone column to retrieve the area code.
SQL SUBSTRING() Syntax
SELECT SUBSTRING (expression, start, length)
FROM table_name
Below is shown the "Authors" table:
White |
Michael |
22 Cleveland Av. #24 |
Berkeley |
Hunter |
Sheryl |
NULL |
Salt Lake City |
Smith |
Charlene |
NULL |
San Francisco |
Jhonson |
Fernandis |
2286 Cram Pl. #81 |
NULL |
Robert |
Paul |
1976 Arlington Pl. |
NULL |
SQL SUBSTRING() Example
Text Editor below has the SQL Query, click the "View Result" button, to see the result.
To retrieve a part of the string from the FirstName column in the Authors table.
Select SUBSTRING(FirstName,2,4) as SubstringPart From Authors;
Note: The substring retrieves the string part from the 2nd position onwards for 4 characters.
SQL in Text Editor: