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.


One Response to “Remove or Trim First or Last Few Characters in MySQL Database with SQL”

  1. Susan Douthart
    October 29th, 2009 03:06
    1

    Hi! I have a column of numbers that are in ascending order:

    ‘1005849011050
    ‘1005849011051
    ‘1005849011052
    ‘1005849011053
    ‘1005849011054
    ‘1005849011055
    ‘1005849011056
    ‘1005849011057

    They are all led by an apostrophe that I’d like to remove. How would I use the trim statement to do this? THANKS!

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 - mysql trim text - sql remove characters - sql remove character from string - mysql strip characters - sql select last character - mysql remove characters - sql remove char - sql remove character - mysql remove last character - pl sql remove last character - trim command in sql - mysql remove first char - sql trim char - remove character from string in sql - sql remove right characters - mysql remove character - sql remove character in string - sql find last character - trim mysql - mysql strip trailing character - sql remove leading characters - sql function to remove characters - trim characters in sql - TRIM CHAR SQL - sql removing characters from string - mysql remove character from string - remove characters sql - trim characters SQL - SQL trim to remove a letter - trim characters before sql - mysql trim last character - mysql last character - sql drop last character - How can i remove the nth character in a field in SQL - mysql replace last character in string - mysql remove characters from string - remove characters sql phone - sql trim after x - mysql remove string - remove leading character sql - remove a string in sql+insert - sql remove last character - SQLServer trim last characters - sql select strip string - trim 1st letter mssql - sql remove first 3 characters - mysql trim - mysql trim string - remove string sql -