RODBC - 导入表格

时间:2017-10-20 10:43:08

标签: sql r database

我想导入一个表,但是我在向量“Daty”中有一个动态日期。我的问题是我无法导入一个日期为变量的表。

                select
                    Symbol
                ,   OpenTime
                from
                    xxx t
                inner join
                    zzz i
                on
                    t.xxxxx = i.zzzzzz
                where
                    OpenTime between '",Daty[1],"' and '",,"'
                and Symbol like '%xxx%'

当我这样做时:

x <- sqlQuery(ch, query)

R无法导入此表格。

1 个答案:

答案 0 :(得分:1)

变量query必须是字符串。在查询中包含变量的一种方法是使用paste0()

query <- paste0(
  "select
    Symbol
  , OpenTime
  from
  xxx t
  inner join
  zzz i
  on
  t.xxxxx = i.zzzzzz
  where
  OpenTime between '",
  Sys.Date(), # first date Daty[1] in your case
  "' and '",
  Sys.Date() + 1, # second date
  "' and Symbol like '%xxx%'"
)

返回(使用cat(query)):

select
    Symbol
  , OpenTime
  from
  xxx t
  inner join
  zzz i
  on
  t.xxxxx = i.zzzzzz
  where
  OpenTime between '2017-10-20' and '2017-10-21' and Symbol like '%xxx%'