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.

Share and contribute or get technical support and help at My Digital Life Forums.



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 without commenting


Custom Search

New Articles

Incoming Search Terms for the Article

mysql trim - sql trim characters - SQL REmove characters - sql trim last character - sql remove first character - sql replace first character - remove characters sql - trim characters sql - mysql select trim - php remove first character - SQL remove last character - mysql remove character - remove char sql - remove characters in sql - sql trim - mysql strip last char - sql remove char - php remove first character from string - trim characters in access - query for selecting first three characters of a string - deleting characters in sql - mysql remove first letter - php mysql trim - trim sql command - how to remove last charater in query + sql server 2005 - mysql replace first character - mysql trim spaces - php remove first character of string - remove first character sql - remove last character sql - remove characters MySQL Query - remove character SQL - replace character command in mysql - sql remove charecters matching another column - sql select trim - sql remove first and last character - sql server remove last char - trim characters - trim mysql - how to remove last character in mysql - php remove first characters - javascript strip first character - mysql query trim - mysql sql replace character - how to remove first character in string using php - mysql delete last character - delete last character of field sql statement - sql deleting characters - select last few characters from text - sql replace leading characters -