Pages

Saturday, February 6, 2010

Convert FLOAT to VARCHAR

If you try to convert a float variable to varchar
by using CAST  or CONVERT, you will get an error message:

DECLARE @Float FLOAT
SET @Float=1.123456789
SELECT CAST(@Float AS VARCHAR(4))

Msg 232, Level 16, State 2, Line 4
Arithmetic overflow error for type varchar, value = 1.123457.

The problem is that the CAST and CONVERT will convert
float/real value to varchar with length 6 and above.

In our case the STR function can help:
SELECT STR(@Float,6,5)

See the BOL for more information.

No comments:

Post a Comment