PostgreSQL

How do I Trim a String in PostgreSQL?

We sometimes want to store our data in the form of a string rather than as integers or any other frequently used data type. In such cases, we wish to have all those functions and commands available with which we can work with the strings easily regardless of which programming language we are using at the moment. PostgreSQL also supports the string data type and presents us with a wide range of functions that can be used with the strings. Sometimes, we want to trim a specific part of a string, and for learning how this can be made possible, you will have to take time out to go through this article.

Trimming a String in PostgreSQL in Windows 10:

There are different functions available for trimming the strings in PostgreSQL in Windows 10. The functionality of these functions differs in terms of the positions from which the specified characters are trimmed, i.e., beginning, end, or both. By viewing all the examples that we have explained below, you will learn the different methods of trimming a string in PostgreSQL in Windows 10.

Example # 1: Trimming a String in PostgreSQL from the Beginning:

If you want to trim the desired string in PostgreSQL from the beginning, then you will have to run the following query:

# SELECT trim(leading123from ‘123Aqsa’);

In this query, we have used the “trim” function that takes the “trimming position,” i.e., leading in this case, “character to be trimmed” i.e., 123 in this case, and the “string” i.e., 123Aqsa in this case as input. The “leading” keyword specifies the position from which the specified character will be trimmed from the string. In this example, we just wanted to remove all the numbers from the beginning of the string while keeping the name intact.

The image shown below represents the output of this query:

Example # 2: Trimming a String in PostgreSQL from the End:

If you want to trim the desired string in PostgreSQL from the end, then you will have to run the following query:

# SELECT trim(trailing123from ‘Aqsa123’);

In this query, we have used the “trim” function that takes the “trimming position” i.e., trailing in this case, “character to be trimmed” i.e., 123 in this case, and the “string” i.e., Aqsa123 in this case as input. The “trailing” keyword specifies the position from which the specified character will be trimmed from the string. In this example, we just wanted to remove all the numbers from the end of the string while keeping the name intact.

The image shown below represents the output of this query:

Example # 3: Trimming a String in PostgreSQL from the Beginning and the End:

If you want to trim the desired string in PostgreSQL from the beginning as well as from the end, i.e., from both the sides, then you will have to run the following query:

# SELECT trim(both123from ‘123Aqsa123’);

In this query, we have used the “trim” function that takes the “trimming position” i.e., both in this case, “character to be trimmed” i.e., 123 in this case, and the “string” i.e., 123Aqsa123 in this case as input. The “both” keyword specifies the position from which the specified character will be trimmed from the string. In this example, we just wanted to get rid of all the numbers from the beginning as well as from the end of the string while keeping the name intact.

The image shown below represents the output of this query:

Example # 4: Trimming a String in PostgreSQL without Specifying the Character to be trimmed:

You can also choose to trim a string in PostgreSQL without explicitly specifying the character to be trimmed. In that case, space will be considered as the default character to be trimmed. The following query depicts this:

# SELECT trim(leading from ‘ Aqsa’);

In this query, we have specified the “trimming position” i.e., leading, in this case, but we have not specified any character to be trimmed. We just wanted to remove the empty space at the beginning of the string while keeping the name intact.

The image shown below represents the output of this query:

Example # 5: Trimming a String in PostgreSQL without Specifying the Character to be trimmed and the Trimming Position:

You can even trim a string in PostgreSQL without specifying the character to be trimmed as well as without specifying the trimming position. In that case, space will be considered the default character to be trimmed, whereas “both” will be considered the default “trimming position” i.e., your desired string will be trimmed from both ends. The following query depicts this:

# SELECT trim(‘ Aqsa ’);

We neither had the “trimming position” nor the “character to be trimmed” in this query. We simply wanted to remove the empty spaces at the beginning and the end of the string while keeping the name intact.

The image shown below represents the output of this query:

Note: The following three examples are the alternative methods of executing the queries that we have used in our first three examples.

Example # 6: Using the “ltrim” Function in PostgreSQL:

To trim a string from the beginning, you can also use another function which is depicted by the following query:

# SELECT ltrim(‘MADAM’, ‘M’);

In this query, we have used the “ltrim” function that accepts two arguments, i.e., the string (MADAM in this case) and the character to be trimmed from the string (M in this case). This function attempts to remove the specified character from the beginning of a string.

The image shown below represents the output of this query:

Example # 7: Using the “rtrim” Function in PostgreSQL:

To trim a string from the end, you can also use another function which is depicted by the following query:

# SELECT rtrim(‘MADAM’, ‘M’);

In this query, we have used the “rtrim” function that accepts two arguments, i.e., the string (MADAM in this case) and the character to be trimmed from the string (M in this case). This function attempts to remove the specified character from the end of a string.

The image shown below represents the output of this query:

Example # 8: Using the “btrim” Function in PostgreSQL:

To trim a string from the beginning as well as from the end, you can also use another function which is depicted by the following query:

# SELECT btrim(‘MADAM’, ‘M’);

In this query, we have used the “btrim” function that accepts two arguments, i.e., the string (MADAM in this case) and the character to be trimmed from the string (M in this case). This function attempts to remove the specified character from the beginning and from the end of a string.

The image shown below represents the output of this query:

Conclusion:

This article was all about trimming a string in PostgreSQL in Windows 10. We talked about all the different functions that can be used to trim a string in PostgreSQL in Windows 10. These functions were basically intended to specify the position from where you want to trim a particular string. By using these different functions, you can conveniently trim any desired string from the beginning, end, or both sides simply by picking the correct function that will serve the intended purpose for you.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.