Remove or Trim First or Last Few Characters in MySQL Database with SQL
Another useful string function in MySQL database is TRIM() which will return a text string after removing the matching leading or trailing characters, also known as prefixes or suffixes. It’s been described by MySQL reference as function that returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING, or TRAILING is given, BOTH is assumed. remstr is optional and, if not specified, spaces are removed.
Syntax of TRIM():
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)
For example:
SELECT TRIM(’ bar ‘);
will return ‘bar’ (spaces dropped).
SELECT TRIM(LEADING ‘x’ FROM ‘xxxbarxxx’);
will return ‘barxxx’ (only leading x characters is removed).
SELECT TRIM(BOTH ‘x’ FROM ‘xxxbarxxx’);
will return ‘bar’ (leading and trailing xs is dropped).
SELECT TRIM(TRAILING ‘xyz’ FROM ‘barxxyz’);
will return ‘barx’ (trailing xyz is cleared).
This function is multi-byte safe. And it can also be used with other SQL command such as UPDATE to perform modification update directly on database table data with SQL statements using tool such as phpMyAdmin.
Related Articles
- How to Find and Replace Text in MySQL Database using SQL
- Check and Optimize MySQL Database Automatically with Crontab/Cron
- MySQL Database Performance Tuning Best Practices Video Tutorial
- Enable Logging of Slow Queries (Slow Query Log) in MySQL Database
- How to Remove and Drop Datafiles from Tablespace in Oracle Database
- Uninstall and Remove Multiple Database Instances of Microsoft SQL Server 2005
- How to Escape Characters in Oracle PL/SQL Queries
- Change and Reset MySQL root Password
- WordPress MySQL SQL Query Error in WPDB Class
- Change or Set MySQL Long Query Time Value for log-slow-queries









































