如何将输出格式更改为货币?

How do I change output format to currency?

我正在尝试使以下 sqlcmd 输出美元货币的总金额并删除不必要的尾随 0

declare @today datetime = '2015-5-2'  set nocount on;select [Day Total]  = sum(case when AccountingDate > dateadd(DAY, -1, @today) then [TotalJackpotAmount] else 0 end),        [Week Total] = sum(case when AccountingDate > dateadd(WEEK, -1, @today) then [TotalJackpotAmount] else 0 end),        [Month Total] = sum(case when AccountingDate > dateadd(MONTH, -1, @today) then [TotalJackpotAmount] else 0 end),         [Year Total] = sum([TotalJackpotAmount]) from   [Accounting].[dbo].[HandPay] where [AccountingDate] <= @today and [AccountingDate] > dateadd(year, -1, @today);" -o \support\c$\inetpub\wwwroot\handpaytest\index.html -h-1 -s "," -w 700 -W

您可以像这样将 field/sum 转换为十进制 (18,2):

select convert(decimal(18,2),1.235) 

这将输出 1.24

您的查询将如下所示:

declare @today datetime = '2015-5-2'  
set nocount on;
select 
[Day Total]  = convert(decimal(18,2),sum(case when AccountingDate > dateadd(DAY, -1, @today) then [TotalJackpotAmount] else 0 end)),        
[Week Total] = convert(decimal(18,2),sum(case when AccountingDate > dateadd(WEEK, -1, @today) then [TotalJackpotAmount] else 0 end)),        
[Month Total] = convert(decimal(18,2),sum(case when AccountingDate > dateadd(MONTH, -1, @today) then [TotalJackpotAmount] else 0 end)),         
[Year Total] = convert(decimal(18,2),sum([TotalJackpotAmount]) )
from   [Accounting].[dbo].[HandPay] 
where [AccountingDate] <= @today and [AccountingDate] > dateadd(year, -1, @today);

我看到您已经接受了使用 CONVERT() 的回答。所以你知道,如果你在 SQL 2012 年或更高版本,你可以使用 FORMAT(),如果你还 want/need 查看货币符号,这将很有帮助。

语法

FORMAT ( value, format [, culture ] )

例子

代码:

SELECT FORMAT ( 21000.15000100, 'C', 'en-US' ) AS 'US English Result'
      ,FORMAT ( 21000.15000100, 'C', 'en-gb' ) AS 'Great Britain English Result'
      ,FORMAT ( 21000.15000100, 'C', 'de-de' ) AS 'German Result'
      ,FORMAT ( 21000.15000100, 'C', 'zh-cn' ) AS 'Chinese (Simplified PRC) Result';

输出:

来源

FORMAT (Transact-SQL)