如何转换包含货币符号的varchar price列然后求和?

时间:2014-11-04 13:45:40

标签: mysql sql aggregate-functions string-conversion

我有一个名为tbl_procie的表,它具有以下结构:

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

数据:

id  price
 1   $2.5
 2   $5.3

我希望能够SUM()价格,但目前它还包含货币符号的varchar列。我应该得到答案7.8

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

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

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

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

答案 2 :(得分:2)

尝试以下:

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