Home > Net >  sql server - error converting float to varchar
sql server - error converting float to varchar

Time:09-28

I have a float field, I need to convert it as varchar and I need to add a leading zero when it contains only one nummber(it can contains 1 or 2 numbers). This is my query:

SELECT FORMAT(((LTRIM(RTRIM(STR(floatField,2))))), '00')

but I get an error:data type for argument 1 is not valid. I know I can do this in a different way, but I want to understand what's wrong in this

CodePudding user response:

format takes a float (among other things) and converts it to a varchar. Don't try to do its job for it:

SELECT FORMAT(myfloat, '00') 
FROM   mytable

DBFiddle Demo

  • Related