How to Trim Strings

In order to trim spaces Oracle provides 3 main functions: trim which removes spaces from the start and the end of any string. The functions ltrim and rtrim allow to remove spaces only in the start(left) or the end(right) of the string.

select trim(‘ hello    ‘), ltrim(‘ hello    ‘), rtrim(‘ hello    ‘);
trim ltrim rtrim
“hello” “hello “ “ hello”

However the trim function not only removes spaces, You can specify the character to trim and where you want to trim, leading, trailing or both.

select trim( leading ‘-’ from ‘-hello------’) as lead_trim_result,
       trim( trailing ‘-’ from ‘-hello------’) as trail_trim_result;
lead_trim_result lead_trim_result
“hello------” “-hello”