LTRIM and RTRIM Functions in SQL

For Complete YouTube Video: Click Here

In this class, we will understand LTRIM and RTRIM Functions in SQL.

We have already discussed the concepts of LPAD and PAD functions.

LTRIM and RTRIM Functions in SQL

LTRIM and RTRIM will trim off the unwanted character from the left or right ends of the strings.

LTRIM and RTRIM Syntax

LTRIM(String, [‘set’]);

LTRIM is the name of the function.

The string is the name of the column, or it can be just the string.

Set will be provided with characters that are to be trimmed.

RTRIM(String, [‘set’]);

RTRIM is the name of the function.

The string is the name of the column, or it can be just the string.

Set will be provided with characters that are to be trimmed.

Examples of LTIM and RTRIM

To understand this, we will consider the table shown below.

LTRIM and RTRIM Functions in SQL 1
LTRIM and RTRIM Functions 1

The below are the queries and the outputs with the use of LTRIM and RTRIM.

SELECT LTRIM(name, ‘”‘) FROM students;

LTRIM and RTRIM Functions in SQL 2
LTRIM and RTRIM Functions in SQL 2

SELECT RTRIM(name, ‘.”‘) FROM students;

LTRIM and RTRIM Functions in SQL 3
LTRIM and RTRIM Functions 3

SELECT LTRIM(RTRIM(name, ‘.”‘), ‘”‘) FROM students;

LTRIM and RTRIM Functions in SQL 4
LTRIM and RTRIM Functions 4