[英]How not round up the value when adding or casting it to Money?
SELECT cast(cast(Ac.Amount as decimal(18,2)) as varchar)FROM Assessmentsurchargedetails AC
Result is: 5010.00 27.6 100.00 1000.00 10000.00 结果是:5010.00 27.6 100.00 1000.00 10000.00
i just want to add comma separated when it hits a thousand. 我只想添加逗号分隔符(当它达到1000时)。
You can accomplish this in three ways: 您可以通过三种方式完成此操作:
Here are some examples 这里有些例子
--Integer Conversion Method
SELECT cast(cast(cast(Amount * 100 as int) / 100.0 as decimal(18,2)) as varchar) FROM Assessmentsurchargedetails AC
or 要么
-- FLOOR Method
SELECT cast(cast(FLOOR(Ac.Amount*100)/100.0 as decimal(18,2)) as varchar) FROM Assessmentsurchargedetails AC
or 要么
-- Substring Method, not the increased precision on the initial decimal cast
select LEFT(cast(cast(Ac.Amount as decimal(20,4)) as varchar), cast(LOG10(Amount) as int)+4) from Assessmentsurchargedetails ac
I decided to time each of the methods by inserting about 8.25 million rows from the original Assessmentsurchargedetails
table to another table. 我决定对每种方法进行计时,方法是将原始的
Assessmentsurchargedetails
表中的约825万行插入到另一个表中。 The integer conversion method took 18 seconds to process, while the other two took 16 seconds. 整数转换方法需要18秒来处理,而其他两种方法则需要16秒。 I would recommend the FLOOR method over the Substring method, however, because in most cases you will not want to cast the end result into a varchar, and it is a much more expressive way to solve the problem.
但是,我建议使用FLOOR方法而不是Substring方法,因为在大多数情况下,您不希望将最终结果转换为varchar,这是解决问题的一种更具表现力的方法。
Regarding adding a comma, the commenters are correct- use FORMAT without casting to a varchar first. 关于添加逗号,注释者正确使用了FORMAT而不先转换为varchar。 In that case, your final output would be:
在这种情况下,您的最终输出将是:
SELECT FORMAT(cast(FLOOR(Ac.Amount*100)/100.0 as decimal(18,2)), '#,##0.00') FROM Assessmentsurchargedetails AC
REPLACE(CONVERT(varchar(20), (CAST(SUM(table.value) AS money)), 1), '.00', '')
use this REPLACE and CONVERT to get the desired results. 使用此REPLACE和CONVERT获得所需的结果。 'varchar' doesn't support comma.
'varchar'不支持逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.