Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert, cast, or add '$' to a decimal SQL SUM()

What's the simple way to add the dollar sign ('$') or currency to a decimal data type on the following query:

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

Thanks for the help!

like image 537
Jacman Avatar asked Dec 29 '15 18:12

Jacman


People also ask

How do I convert a number to a decimal in SQL?

Use the CAST() function to convert an integer to a DECIMAL data type. This function takes an expression or a column name as the argument, followed by the keyword AS and the new data type. In our example, we converted an integer (12) to a decimal value (12.00).

What is CAST () and convert () functions in SQL Server?

The T-SQL language offers two functions to convert data from one data type to a target data type: CAST and CONVERT. In many ways, they both do the exact same thing in a SELECT statement or stored procedure, but the SQL Server CONVERT function has an extra parameter to express style.


2 Answers

This is just presenting concern and should be done in application layer.

But SQL Server can do it using FORMAT function:

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

LiveDemo

Output:

╔═════════════════╗
║  Total Charges  ║
╠═════════════════╣
║ $21.00          ║
╚═════════════════╝
like image 139
Lukasz Szozda Avatar answered Oct 22 '22 02:10

Lukasz Szozda


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

like image 30
Henrique Tomazela Lopes Avatar answered Oct 22 '22 03:10

Henrique Tomazela Lopes