Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RODBC pulling float as character in SQL Server

I have searched quite a bit and read the package documentation, but can't find a solution for this.

I'm using RODBC 1.3-12 against Microsoft SQL Server with the below details.

DBMS_Name       "Microsoft SQL Server"    
DBMS_Ver        "10.50.6220"           
Driver_ODBC_Ver "03.52"            
Driver_Name     "SQLSRV32.DLL"             
Driver_Ver      "06.01.7601"         
ODBC_Ver        "03.80.0000"  

I'm not sure how to give a reproducable example, since you'd need a DB, but below is code similar to what I'm using.

myConnection<- odbcDriverConnect(connection = "Driver={SQL Server};server=myServerName;database=myDBName;trusted_connection=yes")

myDataFrame <- sqlQuery(myConnection, "select top 100 * from myTable", as.is=TRUE)

Per the documentation, doubles are coming back as numeric, as I'd expect, but float and money come back as character.

Is anyone aware of a way around this?

like image 203
John Tarr Avatar asked Jan 31 '26 12:01

John Tarr


1 Answers

It looks like the as.is argument is causing this (not exactly sure why; I've never set that argument):

R> str(sqlQuery(tcon, qry))
'data.frame':   1 obs. of  4 variables:
 $ DecimalCol: num 9.5
 $ VarcharCol: chr "some text"
 $ FloatCol  : num 8.55
 $ MoneyCol  : num 100
##
R> str(sqlQuery(tcon, qry, as.is = TRUE))
'data.frame':   1 obs. of  4 variables:
 $ DecimalCol: chr "9.50"
 $ VarcharCol: chr "some text"
 $ FloatCol  : chr "8.5540000000000003"
 $ MoneyCol  : chr "99.9500"
like image 99
nrussell Avatar answered Feb 03 '26 06:02

nrussell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!