The TRIM and RTRIM functions for string in SQL are tools that allow you to make changes to the data strings that you can find in the tables. databases. Both functions allow us to solve issues that may arise in the development work and database queries, since the data established is not always in perfect condition, so it is necessary to carry out transformations to meet the objectives. So, so that you continue learning, we want to talk to you about the TRIM and RTRIM functions to string in SQL.
TRIM and RTRIM for string in SQL
Many times it is necessary to make cuts within strings to create new ones that are usable within the requirements of programs that integrate databases. To carry out this work there are several functions, however, below we will talk about two: the TRIM statement and the RTRIM statement.
Get data with TRIM
To begin with, the SQL TRIM statement is used to remove spaces or specific characters, which are set from the beginning to the end of strings or strings. This is used to remove spaces to either side of the strings. In this way, the general TRIM statement syntax is set as follows: TRIM ([caracteres FROM] chain).
Example of use with TRIM
To make it clear to you about the use of the TRIM statement when working with databases, below we bring you a chain that we trim with this statement. This, in turn, has been modified with another SQL function: REPEAT.
SELECT DESC_ PHRASE
TRIM (REPEAT (‘duck’, 12)) DESC_PHRASE2
FROM PHRASES
WHERE_ID PHRASE = 34;
Once this statement is executed, you will be able to visualize the transformation that is performed on the data in a data table, like the one you will see below.
DESC_ PHRASEDESC_ PHRASE2duck duck duck duck duck duck duck duck duck duck duck duck duckduck
Get data with RTRIM and LTRIM
On the other hand, following the previous sentence, two options appear that allow you to carry out the same process, but depending on which side the cut needs to be made. That is how you must use the RTRIM statement to trim the string to the right side and on the other hand, the LTRIM statement to trim the string to the left side. The general syntax of both statements is determined as follows: RTRIM (chain) and LTRIM (chain).
Example of use with RTRIM and LTRIM
So that it is also clear to you how both statements work, we will attach a practical example below, where we will use the two statements in just one set of instructions.
SELECT DESC_ PHRASE
RTRIM (REPEAT (‘duck ‘, 12)) DESC_PHRASE2
LTRIM (RTRIM (CONCAT ( ‘ ‘, REAPEAT (‘duck ‘, 12)))) DESC_PHRASE3
FROM PHRASES
WHERE_ID PHRASE = 34;
Once all these sentences have been executed, you will be able to view the following table with the three required fields.
DESC_ PHRASEDESC_ PHRASE2DESC_ PHRASE3duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck duck
The Big Data Bootcamp awaits you!
Since you know the TRIM and RTRIM functions to string in SQL, you are surely interested in embarking on a path that allows you to learn many more related functions to work on different data. So, we tell you that he Big Data, Artificial Intelligence & Machine Learning Full Stack Bootcamp from awaits you! In less than a year you can become an expert in databases and stand out in the IT sector. Would you like to boost your career and access this world of great employability and high salaries? Don’t miss this opportunity and enter now to request more information!