在SQL Server中的总成本上添加$ dollar符号

时间:2018-05-16 20:44:02

标签: sql-server

在我的总Ex GST费用之前添加$所需的帮助。我试图做'$' + CONVERT(varchar(20)但仍然无效:(

这是我的代码:

SELECT  CONVERT(varchar, DATEADD(dd, - 1, GETDATE()), 103) AS Date, 'Sales Orders' AS Type, COUNT(dbo.SalesOrder.SalesOrderID) AS Orders, SUM(dbo.SalesOrderDetails.QtyOrdered) AS Chairs, 
                     ISNULL(ROUND(SUM(dbo.SalesOrderDetails.ExtendedPrice), 2), 0) AS [Total Ex GST]FROM            dbo.SalesOrder LEFT OUTER JOIN
                     dbo.SalesOrderDetails ON dbo.SalesOrder.SalesOrderID = dbo.SalesOrderDetails.SalesOrderIDWHERE        (dbo.SalesOrder.EntryDate >= CONVERT(char(8), DATEADD(dd, - 1, GETDATE()), 112)) AND (dbo.SalesOrder.EntryDate < CONVERT(char(8), GETDATE(), 112)) AND (dbo.SalesOrder.CustomerID <> 187)

这是输出:

enter image description here

3 个答案:

答案 0 :(得分:2)

FORMAT函数SQL Server(从2012开始)

select format(amountfield,'C')    -- local currency

select format(amountfield,'C', 'en-US')  -- US currency

有关此网站的更多信息,以添加文化

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

或旧版本

declare @amount money = 100.32

select '$' + cast(@amount as varchar(10));

答案 1 :(得分:0)

如果要在值之前/之后添加一些内容,可以使用concat(),如

select CONCAT("$", field)

https://docs.microsoft.com/en-us/sql/t-sql/functions/concat-transact-sql?view=sql-server-2017

答案 2 :(得分:0)

SELECT  CONVERT(varchar, DATEADD(dd, - 1, GETDATE()), 103) AS Date, 'Sales Orders' AS Type, COUNT(dbo.SalesOrder.SalesOrderID) AS Orders, SUM(dbo.SalesOrderDetails.QtyOrdered) AS Chairs, 
                     '$' + ISNULL(ROUND(SUM(dbo.SalesOrderDetails.ExtendedPrice), 2), 0) AS [Total Ex GST]FROM            dbo.SalesOrder LEFT OUTER JOIN
                     dbo.SalesOrderDetails ON dbo.SalesOrder.SalesOrderID = dbo.SalesOrderDetails.SalesOrderIDWHERE        (dbo.SalesOrder.EntryDate >= CONVERT(char(8), DATEADD(dd, - 1, GETDATE()), 112)) AND (dbo.SalesOrder.EntryDate < CONVERT(char(8), GETDATE(), 112)) AND (dbo.SalesOrder.CustomerID <> 187)