Len function on Float in SQLServer gives wrong length

前端 未结 3 399
小鲜肉
小鲜肉 2020-12-22 03:44

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
         


        
3条回答
  •  猫巷女王i
    2020-12-22 04:20

    LEN() works on [N]VARCHAR(), thus you're running into an implicit conversion from FLOAT to VARCHAR

    see this: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/a4ea2bc1-6f2f-4992-8132-f824fe4ffce0/length-of-float-values-in-ms-sql-server-gives-wrong-result?forum=transactsql

    That means that LEN converts the value to VARCHAR before it actually calculates its length. That's because the length you get coincides with the length of your NVarChar value 1.07994e+006.

提交回复
热议问题