SELECT
(CONVERT(VARCHAR(100),
(select SUM(TransactionAmount) as saleamount
from ProductSaleInformation
where StoreCode = 1
and RegionCode = 1
and BillDate Between '2015-06-01' and '2015-10-31') -
(select SUM(TransactionAmount) as saleamount
from ProductSaleInformation
where StoreCode = 1
and RegionCode = 1
and BillDate Between '2015-01-01' and '2015-05-31')) /
(select SUM(TransactionAmount) as saleamount
from ProductSaleInformation
where StoreCode = 1
and RegionCode = 1
and BillDate Between '2015-01-01' and '2015-05-31') + '%') AS SALESGROWTH
需要帮助将float转换为varchar
答案 0 :(得分:1)
您的查询中包含操作/逻辑错误的顺序。
由于括号位置不正确,您将VARCHAR
除以FLOAT
,我怀疑您的意思是先做所有数学运算,最后将结果转换为VARCHAR
来执行字符串连接操作(添加百分号)。
此外,您没有代码格式/缩进功能,这会使更多更容易看到此错误。
试试此更正版本:
SELECT CONVERT(VARCHAR(100),
(
select SUM(TransactionAmount) as saleamount
from ProductSaleInformation
where StoreCode=1 and RegionCode=1
AND BillDate Between '2015-06-01' AND '2015-10-31'
) - (
select SUM(TransactionAmount) as saleamount
from ProductSaleInformation
where StoreCode=1 and RegionCode=1
AND BillDate Between '2015-01-01' AND '2015-05-31'
) / (
select SUM(TransactionAmount) as saleamount
from ProductSaleInformation
where StoreCode=1 and RegionCode=1
AND BillDate Between '2015-01-01' AND '2015-05-31'
)) + '%' AS SALESGROWTH
答案 1 :(得分:0)
要将float
转换为varchar
,您可以使用CONVERT
或CAST
功能。像这样:
DECLARE @float FLOAT = 1.2
SELECT CONVERT(VARCHAR(20), @float)
SELECT CAST(@float AS VARCHAR(20))