1) One way is to delete everything that you do not need:
gsub("^SELECT *| FROM .*", "", sql)
2) Another approach, if we had a data frame with required fields, is to execute the query using sqldf, and then take its names:
library(sqldf) tbl <- data.frame(field1 = 1, field2 = 2, field3 = 3, x = 4, y = 5) toString(names(sqldf(sql)))
source share