Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Removing any leading 0 from all records in a field

Dear all MySQL Friends,
It is me again!
I am facing a problem of sorting out million records of numbers.
Now, I have to remove any leading 0 which appears in a particular field of all records.
So could you suggest any good command to accomplish this task?
It seems that I must use some conditional command such as IF but I am unsure whether there is such thing in MySQL 5.7.18. I think it is an advanced topic, right?
Looking forward to your helpful opinion.
Answers
-
What is the data type of the field? If it is integer or some such numeric format as it should be, then what you have is a display issue not a data issue. If your data type is char or some such character data, then you can use TRIM. Do you want to just remove the leading zeroes in a query or update the table and remove them permanently? You can use TRIM in a query or an update statement. Please see the demonstration below that shows the differences in data types and usage of TRIM on a CHAR column in a query and an update statement.
-- creation of table, columns and data and results:
mysql> use mysql;
Database changed
mysql> create table test_tab (col1 char(10), col2 integer);
Query OK, 0 rows affected (0.35 sec)mysql> insert into test_tab(col1, col2) values ('01', 1);
Query OK, 1 row affected (0.05 sec)mysql> insert into test_tab(col1, col2) values ('0022', 22);
Query OK, 1 row affected (0.04 sec)mysql> insert into test_tab(col1, col2) values ('000333', 333);
Query OK, 1 row affected (0.16 sec)mysql> show columns from test_tab;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| col1 | char(10) | YES | | NULL | |
| col2 | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> select col1, col2 from test_tab;
+--------+------+
| col1 | col2 |
+--------+------+
| 01 | 1 |
| 0022 | 22 |
| 000333 | 333 |
+--------+------+
3 rows in set (0.00 sec)-- usage of TRIM in a query:
mysql> select trim(leading '0' from col1), col2 from test_tab;
+-----------------------------+------+
| trim(leading '0' from col1) | col2 |
+-----------------------------+------+
| 1 | 1 |
| 22 | 22 |
| 333 | 333 |
+-----------------------------+------+
3 rows in set (0.00 sec)-- usage of TRIM in an update statement:
mysql> update test_tab set col1=trim(leading '0' from col1);
Query OK, 3 rows affected (0.11 sec)
Rows matched: 3 Changed: 3 Warnings: 0mysql> select col1, col2 from test_tab;
+------+------+
| col1 | col2 |
+------+------+
| 1 | 1 |
| 22 | 22 |
| 333 | 333 |
+------+------+
3 rows in set (0.00 sec)mysql> drop table test_tab;
Query OK, 0 rows affected (0.17 sec)MySQL>
-
Hello Barbara,
Thanks for your reply.
The mentioned data type is supposed to be a fax number so it is a large positive integer. However, some people may fill in a second number by / or - so it makes them harder to deal with.
My method is to find each of their absolute value but the character / makes it fails.
I will try your method. Thanks.
-
Looks like you need to split the string up. Unless you are using mariaDB then you will need to write your own function to do this (mariaDB has regexp_substr(), mysql does not):
CREATE FUNCTION split_str( str VARCHAR(255), delim VARCHAR(12), pos INT) RETURNS VARCHAR(255) return REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos), LENGTH(SUBSTRING_INDEX(str, delim, pos -1)) + 1), delim, '');
You will need to For example:
mysql> select trim(leading '0' from split_str(replace(x.str, '-', '/'), '/', 1)) faxno -> from (select '0123 456 789 - 0123 456 999' str) x;+--------------+| faxno |+--------------+| 123 456 789 |+--------------+1 row in set (0.00 sec)
NOTE: the "from (...)" on line 2 is just to produce some dummy data separated with a "-"
-
...and here's a way to get the result in one field, still using the split_str function created previously:
select concat(trim(leading '0' from trim(split_str(replace(x.str, '-', '/'), '/', 1))), ' / ', trim(leading '0' from trim(split_str(replace(x.str, '-', '/'), '/', 2))) ) strippedfrom (select '0123 456 789 - 0123 456 999' str) x;+---------------------------+| stripped |+---------------------------+| 123 456 789 / 123 456 999 |+---------------------------+1 row in set (0.00 sec)
All examples will handle "-" and "/" and replace it with a " / "
...so your update statement would look something like this (not tested):
update your_tableset your_column = concat(trim(leading '0' from trim(split_sty(replace(your_column, '-', '/'), '/', 1))), ' / ', trim(leading '0' from trim(split_str(replace(your_column, '-', '/'), '/', 2))) ) ;
You would replace your_table, your_column with your table and your column.
-
Hello Gaz,
Thanks for your reply again!
I just thought MySQL was more advanced than MariaDB!
Let me try your methods.
-
Here's a way to do it without the split_str function, it does exactly the same thing:
select concat(trim(leading '0' from trim( replace(substring(substring_index(replace(x.str, '-', '/'), '/', 1), length(substring_index(replace(x.str, '-', '/'), '/', 0)) + 1), '/', ''))), ' / ', trim(leading '0' from trim( replace(substring(substring_index(replace(x.str, '-', '/'), '/', 2), length(substring_index(replace(x.str, '-', '/'), '/', 1)) + 1), '/', ''))) ) strippedfrom (select '0123 456 789 - 0123 456 999' str UNION select '0888 955 400 / 0888 559 004') x;+---------------------------+| stripped |+---------------------------+| 123 456 789 / 123 456 999 || 888 955 400 / 888 559 004 |+---------------------------+2 rows in set (0.00 sec)