SQL RTRIM Function Example | SQL Server RTRIM()
SQL RTRIM function is used for removing all the leading whitespaces or specified characters from a string, i.e., from the left-hand side of the string.
SQL RTRIM is one of the SQL String Function, which is used to remove the empty spaces from the Right-hand side of the character expression.
SQL RTRIM Function
SQL RTRIM Function is used to return the character expression after removing the empty spaces from the right side of the given expression.
SELECT RTRIM (input_string, [trim_string])
Input_string: It is the string from where the characters have to be removed from the right-hand side.
Trim_string: It is entirely optional. It is mainly used for specifying the characters to be removed. If this parameter is omitted, then all the leading spaces will be removed.
SELECT RTRIM (‘SQL ’);
Here all the trailing spaces are removed from the string.
SELECT RTRIM (‘1232000’, ‘0’);
As Trim_string was specified as 0. So, all trailing 0 was removed from the string.
SELECT RTRIM (‘00012300’, ‘0’);
As Trim_string was specified as 0. So, all trailing 0 were removed from the string but not the leading zeroes because the function used here was RTRIM, which removes only the leading zeroes.
SELECT RTRIM (‘AppDividend2145’, ‘012345’);
In the above example, all combinations of 0 to 5 are listed. By doing this, all the orders of the trailing numbers from the string will be removed.
SELECT RTRIM (‘aabbabcSQLabc’, ‘abc’);
In the above example, ‘abc’ is declared as Trim_string. So, ‘a’, ‘b’, ‘c’ is considered as an individual character. So, all trailing characters of ‘abc’ are removed from the string.
Above were the common examples to make you clear how RTRIM function works. Let’s apply this function to a table.
Previously, in the employee table, ‘#’ was added after the emp_id. Now we want to remove this character from the emp_id and display only the number. So, to do this following query has to be written.
Select RTRIM (Emp_id, ‘#’) AS Emp_id, Emp_name, City, State, Salary from Employee;
Here, you can see that the new table is created by removing trailing ‘#’ special character from the Emp_id.
SQL LTRIM function is used for removing all the leading whitespaces or specified characters from the string, i.e., from the left-hand side of the string.
SQL Server RTRIM() function
SQL Server RTRIM() function returns the string after truncating all trailing blanks.
The following is the syntax of the RTRIM() function.
In this syntax, the input_string is the expression of character or binary data.
It can be the literal string, variable, or column.
The input_string must evaluate to the value of the data type, except for TEXT, NTEXT, and IMAGE, that can be implicitly convertible to VARCHAR. Otherwise, you must use the CAST() function to convert it to the character string explicitly.