开发者

SQL Query Result Problem

开发者 https://www.devze.com 2023-01-18 05:40 出处:网络
I have Two SQL Query Both Return sele开发者_高级运维ct round(convert(float,\'24367.723\'),2) Result:24367.72

I have Two SQL Query Both Return

sele开发者_高级运维ct round(convert(float,'24367.723'),2)

Result:24367.72

Second:

select convert(varchar(20),round(convert(float,'24367.723'),2))

Result:24367.7

Why the Second Query Return exclude the last digit after converting to varchar

Thanks in Advance


By not specifying a style parameter to the convert function you get the default style (0).

i.e. it is equivalent to doing

select convert(varchar(20),round(convert(float,'24367.723'),2), 0)      

The default style for converting from float to varchar displays a maximum of 6 digits.


When working with a float the STR() function usually gives better results according to MSDN as you've more control.

E.g.

select str(convert(float,'24367.723'),8, 2)


Dont use floats, use exact numberics. Something like this

   convert(varchar(20), convert(numeric(20,2), '24367.72'))
0

精彩评论

暂无评论...
验证码 换一张
取 消