Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlQuery: impossible to get the ID number as Character into R?

I'm using the RODBC package inside an ORACLE DATA BASE (DB). Everything is doing very well, but I need to obtain a table from this DB and some variables as character type, not numbers.

So, I made my query like this:

e    ManzResul_VIII<-sqlQuery(con,"select distinct t.fono_id_dis,
            t.id_predio,
            t.co_calle,
            t.nu_casa,
            t.x,
            t.y,
            t.plancheta  from c_araya.proy_dist08_todo t  where nvl(t.fono_tipo_dis, '-') not in ('CLIENTE', 'POTENCIAL', 'CARTERA')  and nvl(t.x, 0) <> 0 ")

Is impossible to get the ID number as Character, this query change the type of my iDs variables from Character to Numeric type (The ID has a zero at the beginning, but has been changed it into a number). I have read the function description, but I can see how to manage it.

Any idea would be really appreciated, thanks in advance!

like image 811
José Bustos Avatar asked May 30 '12 19:05

José Bustos


People also ask

How do I pull SQL data into R?

Query using DBI You can query your data with DBI by using the dbGetQuery() function. Simply paste your SQL code into the R function as a quoted string. This method is sometimes referred to as pass through SQL code, and is probably the simplest way to query your data. Care should be used to escape your quotes as needed.

Can you use SQL code in R?

You can run SQL code in an R Markdown document. Create a sql code chunk and specify your connection with the connection = con code chunk option. R Markdown provides options that simplify using SQL with R.

How do I change data from numeric to character in SQL?

How to convert a numeric field to a character field in OpenEdge SQL? To convert a numeric field to a character value in OpenEdge SQL or Progress SQL-92, use the TO_CHAR scalar function or the CONVERT function: SELECT TO_CHAR (CustNum) FROM PUB.

Can you use R instead of SQL?

R makes performing common data analysis tasks such as loading data, transforming, manipulating, aggregating, charting and sharing your analyses very easy, and the workflow is much more seamless than in SQL.


1 Answers

Change the query to cast the id to the data type you want:

select distinct cast(t.fono_id_dis as varchar(255)) as id
. . . 

This should work with most databases.

like image 112
Gordon Linoff Avatar answered Oct 06 '22 14:10

Gordon Linoff