如何在 SQL Server 中将浮点数转换为 varchar

如何在 SQL Server 中将浮点数转换为 varchar

问题描述:

我有一个包含不同长度数字的浮点列,我正在尝试将它们转换为 varchar.

I have a float column with numbers of different length and I'm trying to convert them to varchar.

有些值超过 bigint max size,所以我不能做这样的事情

Some values exceed bigint max size, so I can't do something like this

cast(cast(float_field as bigint) as varchar(100))

我试过使用十进制,但数字的大小不同,所以这也无济于事

I've tried using decimal, but numbers aren't of the same size, so this doesn't help too

CONVERT(varchar(100), Cast(float_field as decimal(38, 0)))

感谢任何帮助.

更新:

样本值为 2.2000012095022E+26.

尝试使用 STR() 函数.

SELECT STR(float_field, 25, 5)

STR() 函数

另一个注意事项:左边有空格.如果这是一个问题,请与 LTRIM 结合使用:

Another note: this pads on the left with spaces. If this is a problem combine with LTRIM:

SELECT LTRIM(STR(float_field, 25, 5))