Wrong query-result with R
From my 32bit-Windows (Vista) laptop I need R to query an MS SQL Server 2008 database on a 64bit-Windows (Server 2008) on another computer (via internet).
If I use SQL Server Studio on my laptop to connect to the database and send this query:
SELECT * from mytable where id = 1111111111111110032
I get the response
id, ..... 1111111111111110032, .....That is correct. I get the record I ask for.
But if I write this in R:
library(RODBC)
My_conn <- odbcConnect("myODBC_connection", uid="abc123", pwd="abc123")
tbl_Calls <- sqlQuery(My_conn, "SELECT * from mytable where id = 1111111111111110032")
I get the response
id, ..... 1111111111111110128, .....In other words, I get another record than I asked for (one that ends with ...128 instead of ...032). I know it isn't the query itself that is the issue, as it worked from SQL Studio.
I have no clue what could be wrong. Is the 32bit vs 64bit an issue? In my ODBC-connect开发者_StackOverflow社区ion I use "SQL Server" (6.00.6002.18005).
This is probably not an R issue, but I don't know where else to start... Maybe it is because the id is 19 numbers long?
/Chris
I found the problem, and a "dirty" solution. The issue was not in the question, but in a faulty response of the id-field. I.e. the question and received record was correct, except the very long field I was using.
For the benefit of others, here is the solution.
tbl_Calls_2 <- sqlQuery(My_conn, "SELECT id, LEFT(id,10) as LeftId, RIGHT(id,9) as RightId from mytable where id = 1111111111111110032")
attach(tbl_Calls_2)
tbl_Calls_2$CorrectId <- paste(LeftId,RightId,sep="")
detach(tbl_Calls_2)
If anybody knows a nicer solution I'd appreciate it.
/Chris
精彩评论