2015-11-20 100 views
-3
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 

需要幫助的浮動轉換爲VARCHAR轉換錯誤,而在SQL Server中轉換浮動爲varchar

+1

你能編輯這個問題來提供一個[最小,完整,可驗證的例子](http://stackoverflow.com/help/mcve)?所有這些代碼可能沒有必要解決您的問題。 – laylarenee

+0

請發佈錯誤信息。 – Pang

回答

1

您的查詢有它運行/邏輯錯誤的訂單。

您將一個VARCHARFLOAT因放置不當括號,我懷疑你的意思是先完成所有的數學和最終結果轉換爲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 
0

float轉換爲varchar,您可以用CONVERTCAST功能。像這樣:

DECLARE @float FLOAT = 1.2 
SELECT CONVERT(VARCHAR(20), @float) 
SELECT CAST(@float AS VARCHAR(20)) 
相關問題