SQL SUBSTRING()
In SQL, the SUBSTRING() function takes a slice from a string containing data in binary, character, text, or image format. Upon extraction, the SELECT statement can be used to select and manipulate the extracted substring according to the data type. The data type is the same as the original string except for the next expressions.
SQL SUBSTRING() Syntax
SUBSTRING(string, start, length)
Parameters:
string: The string to extract a substring from.start: Thestringindex from which the substring starts. The minimum possible value is1.length: The number of characters to extract fromstring.
Return value:
Returns a substring of the original string. If length exceeds the remaining characters, the result contains everything from start to the end of the string.
Note: Not all services support this function. Oracle and SQLite use
SUBSTR()to accomplish the same goal.
Example 1: Using SUBSTRING() on a String Literal
This query uses SUBSTRING() on a string literal:
SELECT SUBSTRING('Codecademy', 1, 4) AS ExtractString;
Here is the output:
| ExtractString |
|---|
| Code |
Example 2: Using SUBSTRING() on a Table Column
Suppose we have a table named Employees:
| EmployeeID | FullName |
|---|---|
| 1 | John Carter |
| 2 | Alice Johnson |
| 3 | Michael Brown |
This query uses SUBSTRING() on the FullName column of the Employees table:
SELECT FullName, SUBSTRING(FullName, 1, 4) AS FirstFourCharsFROM Employees;
Here is the output:
| FullName | FirstFourChars |
|---|---|
| John Carter | John |
| Alice Johnson | Alic |
| Michael Brown | Mich |
Example 3: Extracting Domain from an Email Using SUBSTRING()
Suppose we have a table Users:
| UserID | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
This query uses SUBSTRING() to extract the domains from the emails in the Email column of the Users table:
SELECT Email, SUBSTRING(Email, CHARINDEX('@', Email) + 1, LEN(Email)) AS DomainFROM Users;
Here is the output:
| Domain | |
|---|---|
| [email protected] | example.com |
| [email protected] | company.org |
| [email protected] | mysite.net |
Frequently Asked Questions
1. What is SUBSTRING() in SQL?
SUBSTRING() returns part of a string, starting at a specified character position, for a given length.
2. How do SUBSTRING() and SUBSTR() differ?
SUBSTRING(): Standard SQL function, supported by many databases.SUBSTR(): Common in Oracle and some versions of MySQL, with similar functionality but slightly different parameter handling.
3. How to extract a substring from a string in SQL?
In MySQL, you can use either SUBSTRING() or SUBSTR(), they work the same way:
SELECT SUBSTRING('Hello World', 7, 5) AS Result;
Or,
SELECT SUBSTR('Hello World', 7, 5) AS Result;
Here is the output:
| Result |
|---|
| World |
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.17 hours
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.5 hours