Here we will learn what is SQLite Rtrim() function and how to use SQLite Rtrim() function to remove specified characters from the ending or right-hand side of string.
Generally in SQLite Rtrim() function is used to remove specified characters from the right-hand side or ending of the string.
In case if we didn’t specify any characters to remove for Rtrim() function, then it will remove only space characters from the ending of the string.
Following is the syntax of SQLite Rtrim() function to remove the ending or right-hand side of specified characters from a string.
In above SQLite rtrim() function syntax, we defined multiple parameters those are
string – Its source string which is used to trim characters from the right-hand side.
trim_character – The trim_character is a character that we need to remove from the ending or right-hand side of the string. It’s Optional.
In case we didn’t mentioned trim_character then rtrim() function will remove the spaces from the ending or right-hand side of the source string.
Now we will see how to use SQLite rtrim() function to remove the ending or right-hand side of characters with examples.
sqlite> SELECT rtrim("--Tutlane-----","-");
rtrim("--Tutlane-----","-")
---------------------------
--Tutlane
If you observe the above result, the SQLite rtrim() function removed specified characters from the right-hand side of string.
Following is another example of using SQLite rtrim() function to remove the right-hand side of characters from a string.
sqlite> SELECT rtrim('123Tutlane123321','123');
rtrim('123Tutlane123321','123')
--------------------------------
123Tutlane
If you observe above result rtrim() function removed “123” characters from right-hand of string.
Following is another example of using SQLite rtrim() function to remove the right-hand side of characters from a string.
sqlite> SELECT rtrim('123Tutlane4153','12345');
rtrim('123Tutlane4153','12345')
--------------------------------
123Tutlane
If you observe the above result, it removed “12345” characters from the right-hand of string using rtrim() function.
Now we will see how to use Rtrim() function in SQLite tables with examples for that we need to create a table called emp_master using the following query.
To create and insert some data in the emp_master table execute the following query.
CREATE TABLE emp_master
(emp_id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT,
last_name TEXT,
salary NUMERIC,
dept_id INTEGER);
INSERT INTO emp_master
values (1,'Honey','Patel', 10100,1),
(2,'Shweta','Jariwala', 19300,2),
(3,'Vinay','Jariwala', 35100,3),
(4,'Jagruti','Viras', 9500,2),
(5,'Shweta','Rana',12000,3),
(6,'sonal','Menpara', 13000,1),
(7,'Yamini','Patel', 10000,2),
(8,'Khyati','Shah', 50000,3),
(9,'Shwets','Jariwala',19400,2);
Now run the following query to check the records of emp_master table.
sqlite> SELECT * FROM emp_master;
emp_id first_name last_name salary dept_id
---------- ---------- ---------- ---------- ----------
1 Honey Patel 10100 1
2 Shweta Jariwala 19300 2
3 Vinay Jariwala 35100 3
4 Jagruti Viras 9500 2
5 Shweta Rana 12000 3
6 Sonal Menpara 13000 1
7 Yamini Patel 10000 2
8 Khyati Shah 50000 3
9 Shwets Jariwala 19400 2
Now write the query like as shown following to remove ending characters “ti” from first name of employees whose first name ends with “ti” from emp_master table.
sqlite> SELECT emp_id,first_name,rtrim(first_name,'ti')
FROM emp_master
WHERE first_name LIKE '%ti';
emp_id first_name rtrim(first_name,'ti')
---------- ---------- ----------------------
4 Jagruti Jagru
8 Khyati Khya
If you observe the above example result, it returns first name column values after trimming the right side of the string with “ti”.
This is how we can use SQLite Rtrim() function to remove the right-side characters from string based on our requirements.