MSSQL float data problem

Hello,

I am connecting to MSSQL through ODBC using regular SQL commands (SQLAllocHandle, SQLConnect, SQLFetch). Sometimes when I retrieve float data from the server it gets corrupted. For instance, instead of 59.457443 I will actually get 59.45744299999998. The value that is stored in the database is 59.457443 and it is stored as a float. The code I use to retrieve the data worked OK with an Access database, the problem only appeared after switching to MSSQL. Does anybody know if it's a configuration issue? Any help would be greatly appreciated.

EoF

[605 byte] By [EoF] at [2008-1-10]
# 1
I did some tracing with ODBC and ODBC fetches the wrong value value from the SQL server, so this is not a Visual C++ connection problem as I originally thought. I will ask in the appropriate forum.
EoF at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2

My guess is that it's being rounded to the nearest milliononth but storing the entire value. Once you relocate the value, the original value is returned.

If you are looking for the desired value, you'll have manually round the value in your code.

Adam

AdamD.Turner at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
Thank you for your reply. I don't think that is the problem as I did some further testing. If I don't use ODBC to connect to the Server but System.Data.SqlClient, I get the correct value, so I guess it is an ODBC problem.
EoF at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4

EoF wrote:
Thank you for your reply. I don't think that is the problem as I did some further testing. If I don't use ODBC to connect to the Server but System.Data.SqlClient, I get the correct value, so I guess it is an ODBC problem.

As I suspected, ODBC may round floats when using SQL Server.

Please read the following article from Microsoft:

http://support.microsoft.com/kb/116332

Adam

AdamD.Turner at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...
# 5
Adam,

thank you for the link, however there are a few issues. I am not using prepared statements but rather SQLExecDirect(). I also tried the workaround specified there but it didn't work.

EoF at 2007-10-3 > top of Msdn Tech,Visual C++,Visual C++ General...