【问题标题】:sqlQuery: impossible to get the ID number as Character into R?sqlQuery:不可能将ID号作为字符输入R?
【发布时间】:2012-06-05 02:27:08
【问题描述】:

我在 ORACLE DATA BASE (DB) 中使用 RODBC 包。一切都做得很好,但我需要从这个数据库中获取一个表和一些变量作为字符类型,而不是数字。

所以,我这样查询:

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 ")

无法将ID号获取为Character,此查询将我的iDs变量的类型从Character类型更改为Numeric类型(ID开头为0,但已将其更改为数字)。功能说明我已经看过了,但是我可以看到如何管理它。

任何想法都将不胜感激,在此先感谢!

【问题讨论】:

  • select 语句在 R 之外是否按预期工作?如果没有,那么mplourde的答案应该有效。如果是,请尝试sqlQuery(con,"select ...",as.is=T)

标签: sql r dataframe rodbc


【解决方案1】:

更改查询以将 id 转换为您想要的数据类型:

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

这应该适用于大多数数据库。

【讨论】:

    【解决方案2】:

    这对我有用:

    library(RODBC)
    con <- odbcDriverConnect('driver={SQL Server};server=[your server name];database=[your database name];trusted_connection=true')
    ManzResul_VIII<-sqlQuery(con,"select distinct ('m' + id) as id from [your table]")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2011-10-18
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多