convert float into varchar in SQL server without scientific notation

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

    There are quite a few answers but none of them was complete enough to accommodate the scenario of converting FLOAT into NVARCHAR, so here we are.

    This is what we ended up with:

    DECLARE @f1 FLOAT = 4000000
    DECLARE @f2 FLOAT = 4000000.43
    
    SELECT TRIM('.' FROM TRIM(' 0' FROM STR(@f1, 30, 2))),
           TRIM('.' FROM TRIM(' 0' FROM STR(@f2, 30, 2)))
    SELECT CAST(@f1 AS NVARCHAR),
           CAST(@f2 AS NVARCHAR)
    

    Output:

    ------------------------------ ------------------------------
    4000000                        4000000.43
    
    (1 row affected)
    
                                   
    ------------------------------ ------------------------------
    4e+006                         4e+006
    
    (1 row affected)
    

    In our scenario the FLOAT was a dollar amount to 2 decimal point was sufficient, but you can easily increase it to your needs. In addition, we needed to trim ".00" for round numbers.

提交回复
热议问题