I am reading text in from a txt file and pass the contents to SQL. The SQL text contains double quotes and is causing problems. I would like to remove the "\" in the string below so I can send it to SQL
test<- "select case when \"est\" dsaf"
test<- cat(test, sep="")
class(test)
returns an UNQUOTED null object
> test<- "select case when \"est\" dsaf"
> test<- cat(test, sep="")
select case when "est" dsaf
> class(test)
[1] "NULL"
When I pass the unquoted string to SQL I get this error:
Error in odbcQuery(channel, query, rows_at_time) :
'getCharCE' must be called on a CHARSXP
and I would like it to return with the leading and trailing quotes then I can send it on to SQl and it will work.
[1] "select case when "est" dsaf"
Perhaps you would like to see a different representation of the same string:
When a character value is built with double quotes, any interior instances of
\"become only double-quotes. They will be displayed byprint(and by the REPL that you see in an interactive session) with the escape-backslash, but usingcatyou cant determine that they are not really in there as backslashes.Further proof:
You can use either
cator print withquote=FALSEin you want to display the value as it really exists internally:This is evidence that at least one version of "SQL" agrees (or "accepts") that there is no backslash when
\"appears in the interior of a string:So the was the first problem. The second problem was trying to assign the value of a
catcall. Thecatfunction always returns NULL after sending its value to a destination, possibly the console output. You cannot save the resulting character value to an R name. You always get NULL. See the?cathelp page.