How do you use the TRIM function in Oracle?

The TRIM function in Oracle is used to remove leading spaces, trailing spaces, or spaces on both sides of a string. It has different ways to be used, such as TRIM(string) which removes spaces from both ends of the string. For example:

SELECT TRIM('   example   ') FROM dual;

输出:'example'

To remove specified characters from the beginning of a string, you can use the TRIM(LEADING trim_characters FROM string) function. For example, to delete leading spaces from a string.

SELECT TRIM(LEADING ' ' FROM '   example') FROM dual;

输出:'example'

The TRIM(TRAILING trim_characters FROM string) function is used to remove specified characters from the end of a string. For example, to remove trailing spaces from a string.

SELECT TRIM(TRAILING ' ' FROM 'example   ') FROM dual;

输出:'example'

Using the TRIM(BOTH trim_characters FROM string) function removes specified characters from both ends of a string. For example, removing spaces from both ends of a string.

SELECT TRIM(BOTH ' ' FROM '   example   ') FROM dual;

输出:'example'

You can choose the appropriate usage of the TRIM function according to your needs.

bannerAds