Remove or Trim First or Last Few Characters in MySQL Database with SQL

»»»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.

By | 2016-12-09T08:40:08+00:00 December 9th, 2016|Categories: Databases|Tags: , , |6 Comments

About the Author:

LK is a technology writer for Tech Journey with background of system and network administrator. He has be documenting his experiences in digital and technology world for over 15 years. Connect with LK through Tech Journey on Facebook, Twitter or Google+.

STAY IN TOUCH

close-link