简体   繁体   中英

How do I convert a varchar price column containing a currency symbol and then sum it?

I have a table called tbl_procie which has the following structure:

CREATE TABLE `tbl_procie` (
    `id` int,
    `price` varchar(25)
);

Data:

id  price
 1   $2.5
 2   $5.3

I want to be able to SUM() the price however it is currently a varchar column also containing currency symbols. I should get the answer 7.8 .

SELECT SUM(REPLACE(price,'$','')) AS cr 
FROM TABLENAME 
WHERE Valet_Company = "Demo Company"

SELECT SUM(price) AS [Price Total] FROM tbl_temp

或者,如果没有自动删除$符号尝试

SELECT SUM(TRIM(LEADING '$' FROM price)) AS [Price Total] FROM tbl_temp

尝试以下:

select sum(cast(price as double)) from tbl_temp

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM