I have a column meta_entry
of type longtext
with two entries in it $1.000.00
and $54.00
When I run SUM(SUBSTRING(meta_value, 2))
I get 55
because it doesn't read the first entry as 1000
rather it reads it as 1
because of the .
after the 1
How can i write a query that will ignore the periods, while still keeping the period at the end of the number (before the cents)?
Given:
mysql> select * from stuff;
+------------+
| meta_entry |
+------------+
| $1.000.00 |
| $54.00 |
+------------+
2 rows in set (0.00 sec)
The following query yields:
mysql> select sum(replace(substring(meta_entry, 2), '.', '')) / 100 from stuff;
+-------------------------------------------------------+
| sum(replace(substring(meta_entry, 2), '.', '')) / 100 |
+-------------------------------------------------------+
| 1054 |
+-------------------------------------------------------+
1 row in set (0.00 sec)
Of course you'll need a bit more if you have to have those cents on the end, in which case this big ole long ugly thing would do what you need:
mysql> select format(cast(sum(replace(substring(meta_entry, 2), '.', '')) as decimal) / 100, 2) as sum from stuff;
+----------+
| sum |
+----------+
| 1,054.00 |
+----------+
1 row in set (0.00 sec)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.