AppDividend
Latest Code Tutorials

SQL RTRIM Function Example | SQL Server RTRIM()

0

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

Content Overview

SQL RTRIM Function is used to return the character expression after removing the empty spaces from the right side of the given expression.

Syntax

SELECT RTRIM (input_string, [trim_string])

Parameters

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.

Example

SELECT RTRIM (‘SQL     ’);

Output

SQL

Here all the trailing spaces are removed from the string.

Query 2

SELECT RTRIM (‘1232000’, ‘0’);

Output

1232

As Trim_string was specified as 0. So, all trailing 0 was removed from the string.

Query 3

SELECT RTRIM (‘00012300’, ‘0’);

Output

000123

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.

Query 4

SELECT RTRIM (‘AppDividend2145’, ‘012345’);

Output

AppDividend

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.

Query 5

SELECT RTRIM (‘aabbabcSQLabc’, ‘abc’);

Output

aabbabcSQL

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.

Table: Employee

Emp_id Emp_name City State Salary
101# Rohit Patna Bihar 30000
201# Shivam Jalandhar Punjab 20000
301# Karan Allahabad Uttar Pradesh 40000
401# Suraj Kolkata West Bengal 60000
501# Akash Vizag Andhra Pradesh 70000

 

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.

QUERY

Select RTRIM (Emp_id, ‘#’) AS Emp_id, Emp_name, City, State, Salary 
from Employee;

Output

Emp_id Emp_name City State Salary
101 Rohit Patna Bihar 30000
201 Shivam Jalandhar Punjab 20000
301 Karan Allahabad Uttar Pradesh 40000
401 Suraj Kolkata West Bengal 60000
501 Akash Vizag Andhra Pradesh 70000

 

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.

RTRIM(input_string)

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.

Recommended Posts

SQL REVERSE Function

SQL LTRIM Function

SQL TRANSLATE Function

SQL Replicate Function

SQL UPPER Function

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.