Definition of PostgreSQL TRIM()
PostgreSQL TRIM() function is used to eradicate spaces or a particular set of characters that may be leading, trailing or be present on both sides of the string. The trim() function can remove the longest string which can have characters in the beginning, at the end or both at the beginning and the end. The default functionality of this function is removing spaces if there is no explicit mention of any characters which are to be removed. This string can be of the data type char, varchar or any text.
Trim( [ leading | trailing | both ] [ trim character ] from string )
This is the basic syntax of trim function. Here the trim function is followed by options like leading, trailing, trim characters. Let us have a look at these in detail. The above syntax can defer in the following ways.
- Trim(Leading from string): This will remove the spaces which are present at the beginning of the string.
- Trim (Trailing from string): This will remove the spaces which are present at the end of the string.
- Trim (Both from string): This will remove the spaces which are present at the beginning and at the end of the string. This can also be done by using the function as simply mentioning Trim (string). This is also the default way in which trim function works.
How does PostgreSQL TRIM() Function Work?
Trim works in the above specified variations. Let us have a look at each of these in detail.
- Trim (leading from string): Using this function the spaces or characters which are present at the starting of given string can be removed.
select TRIM(LEADING FROM ' This is removing leading spaces');
Here the spaces at the start of the string are removed.
Here leading is the keyword. It removes all spaces which are present at the start.
- Trim (Trailing from string): Using this function the spaces or characters which are present at the end of given string can be removed.
select TRIM(Trailing FROM 'This is removing trailing spaces ');
Here the spaces at the end of the string are removed. Trailing is the keyword.
- Trim (Both from string): Using this function the spaces or characters which are present at the end of given string can be removed.
select TRIM(Both FROM ' This is removing trailing spaces ');
Here the spaces from both ends of the string are removed and Both is the keyword.
Examples to TRIM() Function in PostgreSQL
Let us have a look at different examples of TRIM function.
- We can easily remove unwanted characters by making use of trim function.
SELECT TRIM('00 ' FROM ' 00 AAA ') AS TrimmedString;
In this example, we are trimming the extra 0’s which are present in the text. The text is ‘ 00 AAA ‘. We are removing the extra spaces and 00 which are present at the beginning of the string. The output here is being stored in TrimmedString. The screenshot below shows the output that we get once this code is run. It gives only ‘AAA’. The extra 0’s and spaces are removed from the string and stored in the declared variable.
- The RTRIM functions in a similar way to LTRIM. It removes the trailing spaces or characters which are present in the string.
SELECT RTRIM('AAA TTT ') AS TrimmedString;
- The third is the default way which removes spaces from both sides of the string.
SELECT TRIM(Both '00 ' FROM ' 00 AAA 000 ') AS NewString;
This is the function when blank spaces and ‘00’are removed from both ends of the string. The given string has spaces present at both, the start and at the end of the string ‘ 00 AAA 00 ‘. Due to this when trim function is used as it is it does its job of removing the white spaces from both ends of the string and the output that we get is as: ‘AAA’. All unnecessary blank or white spaces are removed from the string.
SELECT TRIM(Trailing '00 ' FROM ' AAA 000 ') AS NewString;
Here the TRIM function trims the extra spaces and ‘00’ which are present at the end of the string or the spaces which are trailing after AAA. It removes them and then outputs the data without the spaces. This output string is then stored in the declared variable. You can observe that spaces at the beginning are as it is as we have not used Leading Trim function here.
The trim function can also be used with an array. We can make use of procedural query. By using declare we can initialize a variable and then in while loop or for loop we can make use of the trim function as and when needed.
Advantages of using TRIM() Functions in PostgreSQL
The trim() function has advantages as below which makes it easy and efficient to use. Some of them are listed below:
- This is the easiest and simplest of the string function which is present in SQL. You can easily use this for any string and as a result we can get rid of unnecessary spaces or characters which are not needed.
- When creating scripts which are lengthy, and it is not feasible to eliminate spaces from a particular string it is efficient to make use of trim() function which removes all spaces from the string.
- It not only works for spaces but user can remove unwanted characters as well
- It can be used along with arrays, stored procedures or simply to remove any spaces. It has multiple purposes.
- It is quick and enhances the performance of the script or procedure where it is being used as this is a built in function.
- It provides variations like removing only spaces from the beginning of the string or from the end of the string.
The trim() function as a result is very helpful and can be used in simple to complex SQL scripts. It provides quick results with enhanced performance. This function is supported by all modern-day tools which are used in data warehouses and data cleansing. All these capabilities of this function make it very important and also is the reason why it is used extensively.
This is a guide to PostgreSQL TRIM(). Here we also discuss the Introduction and how does postgresql trim() function work along with different examples and its code implementation. You may also have a look at the following articles to learn more –