Convert VarChar to Currency in SQL Server

To convert a varchar to currency, the recommended practice is to do the formatting in the front end.

However if that option is not available to you, you can use the following T-SQL code to do the formatting. In the code shown below, we are converting varchar into US Dollar currency.

DECLARE @t TABLE(amount decimal(12,2))
INSERT INTO @t
SELECT 27450 union all
SELECT 2841.2 union all
SELECT 8786723.62 union all
SELECT 8723

SELECT amount,'$'+ CONVERT(varchar(100),
CAST(amount as money),1) as converted_amount
FROM @t

Note : The value must be converted to money datatype before the formatting

OUTPUT

image


About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at www.sqlteam.com and a moderator at www.sql-server-performance.com. His T-sql blog is at http://beyondrelational.com/blogs/madhivanan

1 comment:

albina N muro said...

The geographic area in which a single currency would create the greatest economic benefit. While traditionally each country has maintained its own separate. gold buyers nj