如何在 SQL Server 2008 R2 中将小数转换为科学数

How to convert decimal to scientific in SQL Server 2008 R2

我有一个名为 Product 的 table。 table 看起来像这样:

ID   Product           Volume
1      xyz       4654.000000000000000
2      abc       121.000000000000000

我想用科学记数法表示VolumeVolume 的数据类型为 decimal(20,8)

我该如何进行这种转换?

declare @t decimal(20,8)=132423423421.00000000
print  cast(@t as float)

您可以在 select 语句中将 volume 字段转换为 float,然后您将得到如上所示的科学记数法。

通常,数据的格式化和呈现应该由 UI 在客户端完成,但如果您坚持...

DECLARE @t decimal(20,8) = 4654.000000000000000;
SELECT @t, CONVERT(nvarchar(32), CAST(@t as float), 2)

结果

4654.00000000   4.654000000000000e+003

首先 CAST decimalfloat (请注意,它可能会降低精度),然后 CONVERT floatnvarchar 使用样式 2:

Always 16 digits. Always use in scientific notation.

备注

float 类型只有 15 位精度,因此它无法在不损失精度的情况下存储您的 decimal(20,8)