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.


Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

sql trim characters - sql remove characters - mysql trim - remove characters sql - sql remove character from string - trim characters sql - mysql trim text - sql trim text - sql remove first character - sql remove char - mysql remove last character - sql remove last character - mysql remove characters - sql remove character - trim characters in sql - mysql trim characters - remove characters in sql - mysql select trim - sql replace first character - remove character sql - sql remove last 3 characters - SQL last character - mysql remove character - trim mysql - mysql update trim - remove last character in sql - remove last character sql - trim first character sql - sql remove first letter - trim sql command - update trim mysql - sql trim last character - sql trim char - remove string sql - php remove first character - sql remove string - trim command in sql - sql replace first char - sql remove chars - SQL trim first character - php trim first character - remove first character sql - mysql remove chars - sql last characters - sql command TRIM - sql select first character - replace first character sql - sql commands trim - TSQL remove first character - sql delete characters - how to remove spaces in SQL - last character sql - TRIM([[LOCATION] [remstr] FROM ] str): - mssql trim characters - mysql remove last char - mysql remove trailing character - sql replace last character - sql remove first character from string - sql replace first - mysql trim update - sql select last character - sql remove first char - sql trim last characters - SQL remove characters from a string - mysql update records remove period from last character - sql remove char from string - TRIM LAST CHARACTER SQL - sql update trim - mysql strip last character - php trim first char - sql select remove characters - remove letters sql - SQL strip first characters - sql update remove characters - sql trim character -