Keep the original class of a value imported from SQL to R

Asked

Viewed 23 times

0

I imported a table from an SQL database, but R is automatically converting the values from a column of character for num and I need him to preserve them in class character. It seems that this occurs because the package RODBC behaves similar to Excel.

Table data to be imported:

TABLE_CAT TABLE_SCHEM           TABLE_NAME             COLUMN_NAME DATA_TYPE TYPE_NAME COLUMN_SIZE BUFFER_LENGTH
5  BI_TOXICOLOGIA         dbo TA_HISTORICO_PETICAO             NU_PROCESSO        12   varchar          21            21
6  BI_TOXICOLOGIA         dbo TA_HISTORICO_PETICAO           NU_EXPEDIENTE        12   varchar          21            21

The columns NU_PROCESSO and NU_EXPEDIENTE, are numeric strings, but need to continue in format CHAR.

I used the package RODBC to import the table:

con <- odbcConnect("BISQL", uid = "login", pwd = "senha")
ANVS_base_completa_temp <- sqlFetch(con, "TA_HISTORICO_PETICAO")

In the result the columns are coming as NUM and end up losing data integrity. Even if I convert them again to character.

str(ANVS_base_completa_temp)
'data.frame':   193137 obs. of  15 variables:
 $ NU_PROCESSO            : num  2.54e+16 2.54e+16 2.54e+16 2.54e+16 2.54e+16 ...
 $ NU_EXPEDIENTE          : num  1.47e+08 1.38e+08 1.26e+08 1.13e+08 7.79e+07 ...

Does anyone know how I can import this table without R converting this data? Suddenly using some other package it works?

  • 2

    Tried to: ANVS_base_completa_temp <- sqlFetch(con, "TA_HISTORICO_PETICAO", as.is = TRUE)?

  • I don’t believe it. Something so simple and me here being humiliated.haha.. It worked perfectly. Thank you very much, Neves.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.