MySQL TRIM

The TRIM function in MySQL removes leading and trailing spaces from a string. It is useful for cleaning up text data.

Examples with Tamil Kings

1. Removing Leading and Trailing Spaces

SELECT TRIM('   Raja Raja Chola   ') AS cleaned_name;

Code Explanation: This query removes the leading and trailing spaces from ' Raja Raja Chola ' and returns 'Raja Raja Chola'.

2. Removing Specific Characters

SELECT TRIM(BOTH 'x' FROM 'xxxVijayalaya Cholaxxx') AS cleaned_name;

Code Explanation: This query removes the character 'x' from both ends of 'xxxVijayalaya Cholaxxx' and returns 'Vijayalaya Chola'.

Best Practices

  • Use TRIM to clean up text data before storage or processing.
  • Specify characters to trim only if needed, otherwise, it defaults to whitespace.

Key Takeaways

  • The TRIM function removes leading and trailing spaces from a string.
  • It can also remove specific characters from both ends of a string.