convert float into varchar in SQL server without scientific notation

前端 未结 13 1410
被撕碎了的回忆
被撕碎了的回忆 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:16

    You will have to test your data VERY well. This can get messy. Here is an example of results simply by multiplying the value by 10. Run this to see what happens. On my SQL Server 2017 box, at the 3rd query I get a bunch of *********. If you CAST as BIGINT it should work every time. But if you don't and don't test enough data you could run into problems later on, so don't get sucked into thinking it will work on all of your data unless you test the maximum expected value.

     Declare @Floater AS FLOAT =100000003.141592653
        SELECT CAST(ROUND(@Floater,0) AS VARCHAR(30) ), 
                CONVERT(VARCHAR(100),ROUND(@Floater,0)), 
                STR(@Floater)
    
        SET  @Floater =@Floater *10
        SELECT CAST(ROUND(@Floater,0) AS VARCHAR(30) ), 
                CONVERT(VARCHAR(100),ROUND(@Floater,0)), 
                STR(@Floater)
    
        SET  @Floater =@Floater *100
        SELECT CAST(ROUND(@Floater,0) AS VARCHAR(30) ), 
                CONVERT(VARCHAR(100),ROUND(@Floater,0)), 
                STR(@Floater)
    

提交回复
热议问题