Snippets

Remove the first character in mysql

UPDATE `tablename`
SET `columnname` = 
TRIM(LEADING  'THE CHARACTER YOU WANT TO REMOVE, eg: A'  FROM `columnname`);
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)

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.

mysql> SELECT TRIM('  bar   ');
        -> 'bar'
mysql> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
        -> 'barxxx'
mysql> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');
        -> 'bar'
mysql> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
        -> 'barx'

This function is multi-byte safe.

Source: MySQL 5.0 Reference Manual

One thought on “Remove the first character in mysql

  1. What if I want to remove the first character but I don’t know what it will be? Is there a way to say “trim first character, regardless of what character it is”?

Comments are closed.

Career Guide for Software Developers