) or currency to a decimal data type with th...">

How to convert, distinguish or add "$" to decimal SQL SUM ()

What an easy way to add a dollar sign ('$') or currency to a decimal data type with the following query:

SELECT TOP 100000 SUM ([dbo].[Entry].[Amount]) AS 'Total Charges' 

Thanks for the help!

+7
c # sql sql-server
source share
3 answers

This is simply a matter of concern and should be done at the application level.

But SQL Server can do this with FORMAT :

 SELECT FORMAT(SUM ([Amount]), 'c', 'en-US') AS 'Total Charges' FROM Entry 

LiveDemo

Output:

 ╔═════════════════╗ β•‘ Total Charges β•‘ ╠═════════════════╣ β•‘ $21.00 β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• 
+8
source share

Here is an example using Sql Server 2012

 Declare @Currency float = 1500.00 Select Format(@Currency, 'C', 'en-US') 

https://msdn.microsoft.com/pt-br/library/hh213505(v=sql.110).aspx

+3
source share

If you don't mind the output data type, you can convert the result to varchar and add "$".

 SELECT TOP 100000 '$ ' + convert(varchar(10), SUM ([dbo].[Entry].[Amount])) AS 'Total Charges' 
+1
source share

All Articles