convert float into varchar in SQL server without scientific notation

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

    This works CONVERT(VARCHAR(100), CONVERT(DECIMAL(30, 15), fieldname))

    0 讨论(0)
  • 2020-12-01 14:21

    Try CAST(CAST(@value AS bigint) AS varchar)

    0 讨论(0)
  • 2020-12-01 14:22

    I have another solution since the STR() function would result some blank spaces, so I use the FORMAT() function as folowing example:

    SELECT ':' + STR(1000.2324422), ':' + FORMAT(1000.2324422,'##.#######'), ':' + FORMAT(1000.2324422,'##')
    

    The result of above code would be:

    :      1000 :1000.2324422   :1000
    
    0 讨论(0)
  • 2020-12-01 14:23

    Try this code

    SELECT CONVERT(varchar(max), CAST(1000.2324422 AS decimal(11,2)))

    result:1000.23

    here decimal(11,2):11-total digits count(without point),2-for two digits after decimal point

    0 讨论(0)
  • 2020-12-01 14:24

    This is not relevant to this particular case because of the decimals, but may help people who google the heading. Integer fields convert fine to varchars, but floats change to scientific notation. A very quick way to change a float quickly if you do not have decimals is therefore to change the field first to an integer and then change it to a varchar.

    0 讨论(0)
  • 2020-12-01 14:26

    Try this:

    SELECT REPLACE(RTRIM(REPLACE(REPLACE(RTRIM(REPLACE(CAST(CAST(YOUR_FLOAT_COLUMN_NAME AS DECIMAL(18,9)) AS VARCHAR(20)),'0',' ')),' ','0'),'.',' ')),' ','.') FROM YOUR_TABLE_NAME
    
    1. Casting as DECIMAL will put decimal point on every value, whether it had one before or not.
    2. Casting as VARCHAR allows you to use the REPLACE function
    3. First REPLACE zeros with spaces, then RTRIM to get rid of all trailing spaces (formerly zeros), then REPLACE remaining spaces with zeros.
    4. Then do the same for the period to get rid of it for numbers with no decimal values.
    0 讨论(0)
提交回复
热议问题