Preface
In recent projects, there are dealings with financial aspects. So there are calculations and statistics of some amounts in sql. I also encountered a series of problems with the thousandth sign format.
(1) In sql, if it is Chinese, it is 0, and if it is a value, it is a value
The value here is the thousandth sign directly, so I have done some processing, using the ISNUMERIC function, if the column value is a character, it will output 1, and if it is a number, it will directly output the number.
If you use a value in the thousands sign format, if you force it directly to int or decimal, an error will be reported directly.
Therefore, it is best to give as money to CAST at the same time. The correct way is
select convert(decimal(16,2),cast('5,222.01' as money))
Okay, let's get to the point! In sql, the Chinese will be 0, and the numeric value will be the numeric value
select
sum(
case when ISNUMERIC(convert(decimal(16,2),cast('5,222.01' as money),1))=1
then CONVERT( decimal(16,2),convert(decimal(16,2),cast('5,222.01' as money),1))
else 0 end
)
operation result
(2) How to convert the thousandth sign value into decimal type or int type calculation in sql
select convert(decimal(16,2),cast('5,222.01' as money),1)
(3) SQL converts int type or decimal type to thousandth sign format
select convert(varchar(20),cast('5222.01' as money),1)