How to convert the thousandth sign value into decimal or int calculation in sql. How to deal with the column value, if it is Chinese, it is 0, if it is a value, it is a value

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.

Insert picture description here
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))

Insert picture description here

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

Insert picture description here

(2) How to convert the thousandth sign value into decimal type or int type calculation in sql

Insert picture description here

select convert(decimal(16,2),cast('5,222.01' as money),1)

(3) SQL converts int type or decimal type to thousandth sign format

Insert picture description here

select  convert(varchar(20),cast('5222.01' as money),1)

Guess you like

Origin blog.csdn.net/WenRouDG/article/details/109274754