web-dev-qa-db-ja.com

TSQLは合計をマネーとしてキャストします

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),())
_

返されるもの: 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_をお金の形式にする必要があります

12
JohnZ

これはプレゼンテーション層で行うべきことですが、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 を参照してください。これは以下を返します。

_|          AMOUNT |
-------------------
| $123,254,578.00 |
|      $99,966.00 |
|           $0.00 |
|       $6,275.00 |
|      $18,964.00 |
|       $1,383.36 |
|      $26,622.36 |
_

注: FORMAT() を使用できるため、SQL Server 2012ではこれがはるかに簡単になります。

_;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デモあり

21
Taryn

Bluefeetの答えに加えて、SQL 2012で使用可能なFORMAT関数は次のように実行できます。

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

Cは通貨を意味し、最後の引数は文化です。文化は、アプリケーションを多言語にしたい場合に重要です。これは、ドル(またはユーロ)記号や3桁ごとの区切り記号などを処理するためです。例えば:

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
6
Jamie Le Tual