SQL RIGHT()
The SQL RIGHT()
function extracts a specified number of characters from the right side of a given string. This function is useful when you need to retrieve trailing characters from a text field, such as file extensions, codes, or the last few digits of an ID.
The RIGHT()
function is widely supported in SQL databases, including SQL Server, MySQL, and PostgreSQL.
In this tutorial, we will go through SQL RIGHT()
String function, its syntax, and how to use this function in SQL statements for string operations, with the help of well detailed examples.
Syntax of SQL RIGHT() Function
The basic syntax of the SQL RIGHT()
function is:
RIGHT(string, number_of_characters);
Each part of this syntax has a specific purpose:
- string: The string from which you want to extract characters.
- number_of_characters: The number of characters to extract from the right side of the string.
The RIGHT()
function returns the specified number of characters from the end of the string.
Setup for Examples: Creating the Database and Table
We’ll create a single files
table with fields file_name
, user_id
, and email
to demonstrate the RIGHT()
function examples.
1. First, create a new database called company_data
:
CREATE DATABASE company_data;
2. Select the company_data
database to work with:
USE company_data;
3. Create a table named files
with the fields file_id
, file_name
, user_id
, and email
:
CREATE TABLE files (
file_id INT PRIMARY KEY AUTO_INCREMENT,
file_name VARCHAR(100),
user_id VARCHAR(10),
email VARCHAR(100)
);
4. Insert sample data into the files
table:
INSERT INTO files (file_name, user_id, email)
VALUES
('report.pdf', '101', 'alice@example.com'),
('invoice.docx', '10234', 'bob@example.com'),
('summary.txt', '987654', 'charlie@example.net');
With this setup complete, we can run the RIGHT()
function examples to test and view results in the files
table.
Examples: Using RIGHT() in SQL Queries
We’ll go through examples demonstrating the RIGHT()
function in SQL, using it to extract specific parts of strings in the files
table.
1. Extracting File Extensions
To extract the last three characters (file extensions) from the file_name
column:
SELECT file_name, RIGHT(file_name, 3) AS file_extension
FROM files;
This query returns each file_name
with the last three characters, which is useful for identifying file types by their extensions.
2. Extracting the Last Four Digits of User IDs
To retrieve the last four characters of user_id
values:
SELECT user_id, RIGHT(user_id, 4) AS last_four_digits
FROM files;
This query extracts the last four characters from each user_id
, useful for displaying only part of the ID.
3. Extracting the Domain from Email Addresses
To extract the domain part of each email address, assuming it is the last part after the “@” symbol:
SELECT email, RIGHT(email, CHARINDEX('@', REVERSE(email)) - 1) AS domain
FROM files;
This query reverses the email
string, finds the position of “@”, and then extracts the domain using RIGHT()
.
Reference:
FAQs for SQL RIGHT()
1. What does the SQL RIGHT() function do?
The RIGHT()
function extracts a specified number of characters from the end of a string, returning them as a new string.
2. Can RIGHT() handle NULL values?
If the input is NULL
, RIGHT()
returns NULL
.
3. Is RIGHT() supported in all SQL databases?
Yes, RIGHT()
is supported in major SQL databases, including SQL Server, MySQL, and PostgreSQL.
4. How is RIGHT() different from LEFT()?
RIGHT()
extracts characters from the end of a string, while LEFT()
extracts characters from the beginning.
5. Can RIGHT() be combined with other string functions?
Yes, RIGHT()
can be combined with functions like CONCAT()
, LEN()
, or REVERSE()
for more complex string manipulations.