How to Trim Strings

In order to trim spaces MySQL provides the function trim which removes spaces from the start and/or the end of any string. By default trim removes the specified character (not only space) from both sides of the string, however you can add the keywords leading or trailing to specify just one side of the string.

select trim(‘M’ from ‘MADAM’) as trim_result;
trim_result
“ADA”

You can indicate 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 trail_trim_result
“hello------” “-hello”

IN THIS PAGE