SQLServer 中 Float 上的 Len 函数给出了错误的长度 [英] Len function on Float in SQLServer gives wrong length

查看:25
本文介绍了SQLServer 中 Float 上的 Len 函数给出了错误的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 中使用以下查询.

I am using the below query in SQL Server.

declare @dt float
set @dt = 1079938.05
select @dt AS Val,Convert(nvarchar(20),@dt) AS NVal, len(@dt) AS Len

它的输出是

Val         NVal            Len
1079938.05  1.07994e+006    12

我的问题是:

  1. 'Val' 列显示正确的值.
  2. 'NVal' 列显示奇怪的值,请解释为什么它会这样显示?
  3. 'Len' 显示长度,其实际长度为 10,但显示为 12.请解释为什么显示为 12 而不是 10.

推荐答案

sql server 中的float 可以是 4 或 8 字节.查找详细信息.

A float in sql server can be 4 or 8 byte. Find details.

LEN() 是一个测量字符串长度的函数.所以你想测量值的字符串表示的长度,而不是值本身.

LEN() is a function to measure the lenght of a string. So you want to measure the length of the string representation of the value, not the value itself.

显示值1.07994e+006是科学记数法,有12个字符.这里没有错.

The shown display value 1.07994e+006 is scientific notation and has 12 characters. Nothing wrong here.

您的调用 Convert(nvarchar(20),@dt) 调用 CONVERT() 函数,默认为 FLOAT 和 REAL(此处提供详细信息和其他格式),科学 用于大于 6 位的数字.当您调用 'len(@dt)' 时,同样会隐式发生.由于LEN()的输入必须是一个字符串,所以转换后传递给函数.

Your call Convert(nvarchar(20),@dt) calls the CONVERT()-function with the defaul for FLOAT and REAL(Details and other formats here), which is scientific for numbers larger than 6 digits. The same happens implicitly when you call 'len(@dt)'. As the input of LEN() must be a string, the value is converted and then passed to the function.

你可以做什么:

  • 您可能会考虑转换为 DECIMAL...

另一个选择是首先使用STR()-functionRTRIM() 一起使用.

Another choice was first to use STR()-function together with RTRIM().

另一种选择是 FORMAT()-功能(SQL Server 2012+).

One more choice was FORMAT()-function (SQL Server 2012+) .

无论如何,您必须考虑到,您看到的文本并不是真正的价值.

Anyway you have to consider, that the text you see is not the real value.

这篇关于SQLServer 中 Float 上的 Len 函数给出了错误的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆