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 dilema 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.
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 dilema 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.