Question

Connecting to a database via RMySQL I wrote some R-scripts - to make them easier: Is it possible to set the date in MySQL Query as a variable?

e.g.

x<-"2012-12-01"

rs <- dbGetQuery(db,"**** where date between 'x' and '2012-12-31'")

Is there any possibility or is everything in "" just MySQL stuff? Thanks!

Was it helpful?

Solution

Use paste to dynamically write your query:

x<-'2012-12-01'
query <-  paste("**** where date between '",x,"' and '2012-12-31'",sep='')

This will creates a query like:

"**** where date between '2012-12-01' and '2012-12-31'"

Then

 rs <- dbGetQuery(db,query)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top