2013-03-20 357 views
6

使用SQL Server 2008中,我試圖讓一些列的返回回來爲$ XXX,XXX.XXTSQL鑄和金錢

這是我使用的查詢(此查詢然後一些更新只是爲了計算數字,並在年底選擇##tempshow

SELECT 
    CASE GROUPING(s.StoreID) WHEN 1 THEN '' ELSE s.StoreID END [StoreID], 
    CASE GROUPING(p.VendorID) WHEN 1 THEN '' ELSE p.VendorID END [VendorID], 
    SUM(d.Quantity) AS [UnitSold], 
    CAST(SUM(d.amount * d.quantity) AS DECIMAL(18,2)) AS Amount, 
    CAST(SUM(d.Discount) AS DECIMAL(18,2)) AS Discount, 
    CAST(SUM((d.Amount * d.Quantity - d.Discount)) AS DECIMAL(18,2)) AS ExtSold, 
    CAST(SUM(d.Cost * d.Quantity) AS DECIMAL(18,2)) AS Cost, 
    CAST(0 AS DECIMAL(18,2)) AS Profit, 
    CAST(0 AS DECIMAL(18,2)) AS OnHand, 
    CAST(0 AS DECIMAL(18,2)) AS OnHandRetail, 
    CAST(0 AS DECIMAL(18,2)) AS OnHandCost, 
    CAST(0 AS DECIMAL(18,2)) AS ReceivedCost, 
    CAST(0 AS DECIMAL(18,2)) AS ReceivedRetail, 
    CAST(0 AS DECIMAL(18,2)) AS ReceivedQty, 
    CAST(0 AS DECIMAL(18,2)) AS Margin, 
    CAST(0 AS DECIMAL(12,2)) AS TurnOver, 
    CAST(0 AS INTEGER) AS OnOrder 
INTO 
    ##tempshow 
FROM 
    RPTrs s, 
    RPTrsd d, 
    RPIv i, 
    RPProducts p 
WHERE 
    s.ReceiptNO = d.ReceiptNO and 
    s.StoreID = d.StoreID and 
    i.UPC = d.UPC and 
    i.StoreID = d.StoreID and 
    p.ProductID = i.IVProduct and 
    s.StoreID = '01' and 
    s.TRSDate > GETDATE()-20 and 
    p.Service = 0 
GROUP BY 
    GROUPING SETS((s.StoreID,p.VendorID),()) 

將返回: enter image description here

我已經試過

CAST(SUM(d.amount * d.quantity) AS MONEY) AS Amount,

SUM(CAST((d.amount * d.quantity) AS MONEY)) AS Amount,

預計產量(加上其他列與此相同Amount列):

|StoreID | VendorID | UnitSold | Amount 
--------------------------------------------- 
1 | 01  | 0000  | 0  | $0.00 
2 | 01  | am  | 62  | $6,275.00 
3 | 01  | AO  | 58  | $18,964.00 
4 | 01  | payless | 6  | $1,383.36 
5 |  |   | 126  | $26,622.36 

我需要的Amount, Discount, ExtSold, Cost, Profit, OnHandRetail, OnHandCost, ReceivedCost, ReceivedRetail是貨幣格式

+2

也許你感到困惑_data type_和_format_。格式化,例如添加貨幣符號,數千個分隔符和一個小數點,最好留給您的應用程序。 – HABO 2013-03-20 17:31:21

+0

不幸的是,我不能這樣做,因爲它只是一個簡單的報告功能,只需要查詢並輸出查詢內容即可 – JohnZ 2013-03-20 17:38:27

+0

@JohnZ:那麼這個函數可以用一個稍微複雜一點代替嗎?真的,即使可以在T-SQL中實現你想要的功能,它也應該關心格式化。在這種情況下,數據庫應該只關心提供數據。 – 2013-03-20 19:29:26

回答

16

這是應該的表示層上進行,但如果你需要做這在SQL中,你可以使用:

'$'+convert(varchar(50), CAST(amount as money), -1) amount 

下面是一個例子:

;with cte (amount) 
as 
(
    select 123254578.00 union all 
    select 99966.00 union all 
    select 0.00 union all 
    select 6275.00 union all 
    select 18964.00 union all 
    select 1383.36 union all 
    select 26622.36 
) 
select '$'+convert(varchar(50), CAST(amount as money), -1) amount 
from cte 

SQL Fiddle with Demo。這將返回:

​​

注:這將在SQL Server 2012中容易得多,因爲你可以使用FORMAT()

;with cte (amount) 
as 
(
    select 123254578.00 union all 
    select 99966.00 union all 
    select 0.00 union all 
    select 6275.00 union all 
    select 18964.00 union all 
    select 1383.36 union all 
    select 26622.36 
) 
select '$'+FORMAT(amount,'#,0.0000') amount 
from cte 

SQL Fiddle with Demo

+0

bluefeet再次救援。非常感謝你,'$'+ convert(varchar(50),CAST(作爲金錢的金額),-1)金額就像魅力一樣工作 – JohnZ 2013-03-20 19:26:14

+0

-1做什麼? – bonh 2015-05-15 18:36:39

+0

@bonh它將逗號添加到數值中。 – Taryn 2015-05-15 19:24:42

3

添加到bluefeet的回答,在SQL中可用的格式功能2012可以這樣完成:

SELECT FORMAT(12345.6789, 'C', 'en-us') 

Th e C是貨幣,最後一個是文化。如果你希望你的應用程序是多語言的,那麼這種文化就很重要,因爲它處理諸如美元(或歐元)標誌和千位分隔符之類的東西。例如:

SELECT 
FORMAT(12345.6789, 'C', 'en-us') as "USA", 
FORMAT(12345.6789, 'C', 'fr-ca') as "French Canada", 
FORMAT(12345.6789, 'C', 'fr-fr') as "French France", 
FORMAT(12345.6789, 'C', 'hi-in') as "Hindi India" 

會給這樣的結果:

USA   French Canada  French France  Hindi India 
----------- -------------  -------------- -------------- 
$12,345.68  12 345,68 $   12 345,68 €  ₹ 12,345.68