How to cut only the first character in MySQL string?


To cut only the first character, use the substr() function with UPDATE command. The syntax is as follows.

UPDATE yourTableName set yourColumnName=substr(yourColumnName,2);

To understand the above syntax, let us first create a table. The query to create a table is as follows.

mysql> create table CutStringDemo
-> (
-> Value varchar(100)
-> );
Query OK, 0 rows affected (0.66 sec)

Now you can insert some records in the table using insert command. The query is as follows.

mysql> insert into CutStringDemo values(',12,3456');
Query OK, 1 row affected (0.14 sec)

mysql> insert into CutStringDemo values(',23,9867');
Query OK, 1 row affected (0.16 sec)

mysql> insert into CutStringDemo values(',20,3212');
Query OK, 1 row affected (0.12 sec)

mysql> insert into CutStringDemo values(',23456,1234');
Query OK, 1 row affected (0.14 sec)

Now you can display all records from the table using select statement. The query is as follows.

mysql> select *from CutStringDemo;

The following is the output.

+-------------+
| Value       |
+-------------+
| ,12,3456    |
| ,23,9867    |
| ,20,3212    |
| ,23456,1234 |
+-------------+
4 rows in set (0.00 sec)

Now let us cut the first character from the column Value. The query is as follows.

mysql> update CutStringDemo set Value=substr(Value,2);
Query OK, 4 rows affected (0.20 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Now you can check that the first character has been removed from the column Value or not. To display all records from the table, use the SELECT statement. The query is as follows.

mysql> select *from CutStringDemo;

The following is the output displaying that the first character successfully removed.

+------------+
| Value      |
+------------+
| 12,3456    |
| 23,9867    |
| 20,3212    |
| 23456,1234 |
+------------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements