John
John

Reputation: 721

SQL Query Result Problem

I have Two SQL Query Both Return

select 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

Upvotes: 2

Views: 88

Answers (3)

dmajkic
dmajkic

Reputation: 3488

Dont use floats, use exact numberics. Something like this

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

Upvotes: 0

Chris W
Chris W

Reputation: 3314

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)

Upvotes: 0

Martin Smith
Martin Smith

Reputation: 453243

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.

Upvotes: 3

Related Questions