TSQL将总和转换为Money

Joh*_*hnZ 12 currency sum sql-server-2008

使用SQL Server 2008,我试图让其中一些列返回$ xxx,xxx.xx

这是我正在使用的查询(此查询然后一些更新只是为了计算数字并##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),())
Run Code Online (Sandbox Code Playgroud)

哪个回报: 在此输入图像描述

我试过了

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
Run Code Online (Sandbox Code Playgroud)

我需要Amount, Discount, ExtSold, Cost, Profit, OnHandRetail, OnHandCost, ReceivedCost, ReceivedRetail钱币格式

Tar*_*ryn 21

这是应该在表示层上完成的,但是如果你需要在sql中这样做,你可以使用:

'$'+convert(varchar(50), CAST(amount as money), -1) amount
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

;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
Run Code Online (Sandbox Code Playgroud)

请参阅SQL Fiddle with Demo.返回:

|          AMOUNT |
-------------------
| $123,254,578.00 |
|      $99,966.00 |
|           $0.00 |
|       $6,275.00 |
|      $18,964.00 |
|       $1,383.36 |
|      $26,622.36 |
Run Code Online (Sandbox Code Playgroud)

注意:这在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
Run Code Online (Sandbox Code Playgroud)

请参阅SQL Fiddle with Demo


小智 6

除了bluefeet的答案,SQL 2012中可用的FORMAT函数可以像这样完成:

SELECT FORMAT(12345.6789, 'C', 'en-us')
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

将给出以下结果:

USA            French Canada        French France     Hindi India
-----------    -------------        --------------    --------------
$12,345.68     12 345,68 $          12 345,68 €       ? 12,345.68
Run Code Online (Sandbox Code Playgroud)

  • 这应该是答案。 (2认同)