Skip to content

11_SQL_in

mike edited this page May 4, 2018 · 1 revision

Maritimes/Mar.utils - Prepare Vectors for SQL "IN" Statements

May 2, 2018 - Mike McMahon (mike.mcmahon@dfo-mpo.gc.ca)

SQL_in.r

Converts a vector of characters into something that an "IN" statement can use. By default, the vector values are surround by apostrophes, as though they were holding character values. Setting apos=FALSE, removes these, making the result appropriate for numeric values.

> vec = c(1:10)
> vec
 [1]  1  2  3  4  5  6  7  8  9 10
> paste0("AND data IN (",SQL_in(vec),")")
[1] "AND data IN ('1','2','3','4','5','6','7','8','9','10')"

> paste0("AND data IN (",SQL_in(vec,apos = FALSE),")")
[1] "AND data IN (1,2,3,4,5,6,7,8,9,10)"

Clone this wiki locally