Remove decimal from records and sum them using a MySQL query


Use aggregate function SUM() along with REPLACE(). Let us first create a table −

mysql> create table DemoTable857(Price varchar(100));
Query OK, 0 rows affected (1.40 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable857 values('50.60');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable857 values('40.30.20');
Query OK, 1 row affected (0.47 sec)
mysql> insert into DemoTable857 values('10.20');
Query OK, 1 row affected (0.69 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable857;

This will produce the following output −

+----------+
| Price    |
+----------+
| 50.60    |
| 40.30.20 |
| 10.20    |
+----------+
3 rows in set (0.00 sec)

Following is the query to remove the decimal places and sum records −

mysql> select sum(replace(Price,'.','')) AS SumOfALl from DemoTable857;

This will produce the following output −

+----------+
| SumOfALl |
+----------+
| 409100   |
+----------+
1 row in set (0.03 sec)

Updated on: 03-Sep-2019

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements