Maintaining Rows in Cash in SQL Server

Is there a simple built-in way to convert NVARCHAR in the format "$ 1,000.00" and "($ 1,000.00)" to the numeric values ​​1000.00 and -1000.00 respectively?

I am trying to do this in SQL Server or SSIS.

Sending to MONEY gives me an error

"Cannot convert a char value to money. The char value has incorrect syntax.". 

when trying to pass a negative value, I guess because of the parenthesis.

+6
source share
1 answer

That should do the trick

 SELECT '$1,000.00' ,CAST('$1,000.00' AS MONEY) ,CAST(REPLACE(REPLACE('($1,000.00)', '(', '-'), ')','') AS MONEY) 

SQL script example

And for the @mellamokb suggestion, if you are using SQL 2012, you can use this:

 SELECT PARSE('($1000.00)' AS MONEY) 

SQL script example

+11
source

Source: https://habr.com/ru/post/915723/


All Articles