convert float into varchar in SQL server without scientific notation

前端 未结 13 1370
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 13:46

convert float into varchar in SQL server without scientific notation and trimming decimals.

for Ex:

i have float value 1000.2324422, then it

相关标签:
13条回答
  • 2020-12-01 14:28

    Below is an example where we can convert float value without any scientific notation.

    DECLARE @Floater AS FLOAT = 100000003.141592653
    
    SELECT CAST(ROUND(@Floater, 0) AS VARCHAR(30))
          ,CONVERT(VARCHAR(100), ROUND(@Floater, 0))
          ,STR(@Floater)
          ,LEFT(FORMAT(@Floater, ''), CHARINDEX('.', FORMAT(@Floater, '')) - 1)
    
    SET @Floater = @Floater * 10
    
    SELECT CAST(ROUND(@Floater, 0) AS VARCHAR(30))
          ,CONVERT(VARCHAR(100), ROUND(@Floater, 0))
          ,STR(@Floater)
          ,LEFT(FORMAT(@Floater, ''), CHARINDEX('.', FORMAT(@Floater, '')) - 1)
    
    SET @Floater = @Floater * 100
    
    SELECT CAST(ROUND(@Floater, 0) AS VARCHAR(30))
          ,CONVERT(VARCHAR(100), ROUND(@Floater, 0))
          ,STR(@Floater)
          ,LEFT(FORMAT(@Floater, ''), CHARINDEX('.', FORMAT(@Floater, '')) - 1)
    
    SELECT LEFT(FORMAT(@Floater, ''), CHARINDEX('.', FORMAT(@Floater, '')) - 1)
          ,FORMAT(@Floater, '')
    

    In the above example, we can see that the format function is useful for us. FORMAT() function returns always nvarchar.

    0 讨论(0)
提交回复
热议问题