从 SQL Server 中的十进制中删除尾随零 [英] Remove trailing zeros from decimal in SQL Server
问题描述
我有一列 DECIMAL(9,6)
即它支持像 999,123456 这样的值.
I have a column DECIMAL(9,6)
i.e. it supports values like 999,123456.
但是当我插入像 123,4567 这样的数据时,它变成了 123,456700
But when I insert data like 123,4567 it becomes 123,456700
如何去除那些零?
推荐答案
A decimal(9,6)
在逗号的右侧存储 6 位数字.是否显示尾随零是一种格式决定,通常在客户端实现.
A decimal(9,6)
stores 6 digits on the right side of the comma. Whether to display trailing zeroes or not is a formatting decision, usually implemented on the client side.
但是由于 SSMS 格式 float
没有尾随零,您可以通过将 decimal
转换为 float
来删除尾随零:
But since SSMS formats float
without trailing zeros, you can remove trailing zeroes by casting the decimal
to a float
:
select
cast(123.4567 as DECIMAL(9,6))
, cast(cast(123.4567 as DECIMAL(9,6)) as float)
印刷品:
123.456700 123,4567
(我的小数点分隔符是逗号,但 SSMS 将小数点格式化为带点.显然是 已知问题.)
(My decimal separator is a comma, yet SSMS formats decimal with a dot. Apparently a known issue.)
这篇关于从 SQL Server 中的十进制中删除尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!