Remove backslashes from character string










3















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"









share|improve this question



















  • 2





    Believe it or not there is no backslash character in "select case when "est" dsaf". Please read ?Quotes

    – 42-
    Jan 28 '15 at 23:41












  • @Richard - I cannot change them to single quotes they need to be double. Any idea how to eliminate the ""

    – user3022875
    Jan 28 '15 at 23:55











  • The cat only provides a side-effect and returns a NULL object.

    – 42-
    Jan 29 '15 at 0:20











  • what do you need to do in sql that you can't do in r?

    – rawr
    Jan 29 '15 at 0:31











  • Provide complete code that precedes the error. (If you are using the result from cat, then test is NULL.)

    – 42-
    Jan 29 '15 at 0:32
















3















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"









share|improve this question



















  • 2





    Believe it or not there is no backslash character in "select case when "est" dsaf". Please read ?Quotes

    – 42-
    Jan 28 '15 at 23:41












  • @Richard - I cannot change them to single quotes they need to be double. Any idea how to eliminate the ""

    – user3022875
    Jan 28 '15 at 23:55











  • The cat only provides a side-effect and returns a NULL object.

    – 42-
    Jan 29 '15 at 0:20











  • what do you need to do in sql that you can't do in r?

    – rawr
    Jan 29 '15 at 0:31











  • Provide complete code that precedes the error. (If you are using the result from cat, then test is NULL.)

    – 42-
    Jan 29 '15 at 0:32














3












3








3








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"









share|improve this question
















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"






r r-faq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 22 '17 at 22:29









Gregor

64.5k990173




64.5k990173










asked Jan 28 '15 at 23:38









user3022875user3022875

2,5571056102




2,5571056102







  • 2





    Believe it or not there is no backslash character in "select case when "est" dsaf". Please read ?Quotes

    – 42-
    Jan 28 '15 at 23:41












  • @Richard - I cannot change them to single quotes they need to be double. Any idea how to eliminate the ""

    – user3022875
    Jan 28 '15 at 23:55











  • The cat only provides a side-effect and returns a NULL object.

    – 42-
    Jan 29 '15 at 0:20











  • what do you need to do in sql that you can't do in r?

    – rawr
    Jan 29 '15 at 0:31











  • Provide complete code that precedes the error. (If you are using the result from cat, then test is NULL.)

    – 42-
    Jan 29 '15 at 0:32













  • 2





    Believe it or not there is no backslash character in "select case when "est" dsaf". Please read ?Quotes

    – 42-
    Jan 28 '15 at 23:41












  • @Richard - I cannot change them to single quotes they need to be double. Any idea how to eliminate the ""

    – user3022875
    Jan 28 '15 at 23:55











  • The cat only provides a side-effect and returns a NULL object.

    – 42-
    Jan 29 '15 at 0:20











  • what do you need to do in sql that you can't do in r?

    – rawr
    Jan 29 '15 at 0:31











  • Provide complete code that precedes the error. (If you are using the result from cat, then test is NULL.)

    – 42-
    Jan 29 '15 at 0:32








2




2





Believe it or not there is no backslash character in "select case when "est" dsaf". Please read ?Quotes

– 42-
Jan 28 '15 at 23:41






Believe it or not there is no backslash character in "select case when "est" dsaf". Please read ?Quotes

– 42-
Jan 28 '15 at 23:41














@Richard - I cannot change them to single quotes they need to be double. Any idea how to eliminate the ""

– user3022875
Jan 28 '15 at 23:55





@Richard - I cannot change them to single quotes they need to be double. Any idea how to eliminate the ""

– user3022875
Jan 28 '15 at 23:55













The cat only provides a side-effect and returns a NULL object.

– 42-
Jan 29 '15 at 0:20





The cat only provides a side-effect and returns a NULL object.

– 42-
Jan 29 '15 at 0:20













what do you need to do in sql that you can't do in r?

– rawr
Jan 29 '15 at 0:31





what do you need to do in sql that you can't do in r?

– rawr
Jan 29 '15 at 0:31













Provide complete code that precedes the error. (If you are using the result from cat, then test is NULL.)

– 42-
Jan 29 '15 at 0:32






Provide complete code that precedes the error. (If you are using the result from cat, then test is NULL.)

– 42-
Jan 29 '15 at 0:32













2 Answers
2






active

oldest

votes


















10














Perhaps you would like to see a different representation of the same string:



test2 <- 'select case when "est" dsaf'
test<- "select case when "est" dsaf"
identical(test, test2)
#[1] TRUE


When a character value is built with double quotes, any interior instances of " become only double-quotes. They will be displayed by print (and by the REPL that you see in an interactive session) with the escape-backslash, but using cat you cant determine that they are not really in there as backslashes.



Further proof:



> nchar(""")
[1] 1


You can use either cat or print with quote=FALSE in you want to display the value as it really exists internally:



> print(test, quote=FALSE)
[1] select case when "est" dsaf


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:



> require(sqldf)
Loading required package: sqldf
Loading required package: gsubfn
Loading required package: proto
Loading required package: RSQLite
Loading required package: DBI
> ?sqldf
> a1r <- head(warpbreaks)
> a1s <- sqldf("select * from warpbreaks limit 6")
Loading required package: tcltk
> a2s <- sqldf("select * from CO2 where Plant like 'Qn%'")
>
> a2sdq <- sqldf("select * from CO2 where Plant like "Qn%"")
> identical(a2s,a2sdq)
[1] TRUE





share|improve this answer




















  • 2





    Do you have an answer?

    – user3022875
    Jan 28 '15 at 23:58






  • 3





    I gave you an answer. There is no backslash to remove.

    – 42-
    Jan 28 '15 at 23:59






  • 1





    I am passing this to string to SQL and it does see the "" that why i need to remove it.

    – user3022875
    Jan 29 '15 at 0:04






  • 1





    You are repeating anthropomorphic characterizations of programmatic behavior. Please provide the requested code and error messages.

    – 42-
    Jan 29 '15 at 0:12






  • 1





    Right. That's what cat is documented to return. You still have not provided any evidence that whatever instance of "SQL" is being communicated with is producing an error related tot he presence of a backslash in the test object.

    – 42-
    Jan 29 '15 at 0:22



















-1














The solution in R - gsub replacing backslashes worked for me.



Example:



library(stringr)
df$variable <- str_replace(df$variable,"\\","")

df$variable before: "xyz"
df$variable after:"xyz"





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28204507%2fremove-backslashes-from-character-string%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














    Perhaps you would like to see a different representation of the same string:



    test2 <- 'select case when "est" dsaf'
    test<- "select case when "est" dsaf"
    identical(test, test2)
    #[1] TRUE


    When a character value is built with double quotes, any interior instances of " become only double-quotes. They will be displayed by print (and by the REPL that you see in an interactive session) with the escape-backslash, but using cat you cant determine that they are not really in there as backslashes.



    Further proof:



    > nchar(""")
    [1] 1


    You can use either cat or print with quote=FALSE in you want to display the value as it really exists internally:



    > print(test, quote=FALSE)
    [1] select case when "est" dsaf


    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:



    > require(sqldf)
    Loading required package: sqldf
    Loading required package: gsubfn
    Loading required package: proto
    Loading required package: RSQLite
    Loading required package: DBI
    > ?sqldf
    > a1r <- head(warpbreaks)
    > a1s <- sqldf("select * from warpbreaks limit 6")
    Loading required package: tcltk
    > a2s <- sqldf("select * from CO2 where Plant like 'Qn%'")
    >
    > a2sdq <- sqldf("select * from CO2 where Plant like "Qn%"")
    > identical(a2s,a2sdq)
    [1] TRUE





    share|improve this answer




















    • 2





      Do you have an answer?

      – user3022875
      Jan 28 '15 at 23:58






    • 3





      I gave you an answer. There is no backslash to remove.

      – 42-
      Jan 28 '15 at 23:59






    • 1





      I am passing this to string to SQL and it does see the "" that why i need to remove it.

      – user3022875
      Jan 29 '15 at 0:04






    • 1





      You are repeating anthropomorphic characterizations of programmatic behavior. Please provide the requested code and error messages.

      – 42-
      Jan 29 '15 at 0:12






    • 1





      Right. That's what cat is documented to return. You still have not provided any evidence that whatever instance of "SQL" is being communicated with is producing an error related tot he presence of a backslash in the test object.

      – 42-
      Jan 29 '15 at 0:22
















    10














    Perhaps you would like to see a different representation of the same string:



    test2 <- 'select case when "est" dsaf'
    test<- "select case when "est" dsaf"
    identical(test, test2)
    #[1] TRUE


    When a character value is built with double quotes, any interior instances of " become only double-quotes. They will be displayed by print (and by the REPL that you see in an interactive session) with the escape-backslash, but using cat you cant determine that they are not really in there as backslashes.



    Further proof:



    > nchar(""")
    [1] 1


    You can use either cat or print with quote=FALSE in you want to display the value as it really exists internally:



    > print(test, quote=FALSE)
    [1] select case when "est" dsaf


    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:



    > require(sqldf)
    Loading required package: sqldf
    Loading required package: gsubfn
    Loading required package: proto
    Loading required package: RSQLite
    Loading required package: DBI
    > ?sqldf
    > a1r <- head(warpbreaks)
    > a1s <- sqldf("select * from warpbreaks limit 6")
    Loading required package: tcltk
    > a2s <- sqldf("select * from CO2 where Plant like 'Qn%'")
    >
    > a2sdq <- sqldf("select * from CO2 where Plant like "Qn%"")
    > identical(a2s,a2sdq)
    [1] TRUE





    share|improve this answer




















    • 2





      Do you have an answer?

      – user3022875
      Jan 28 '15 at 23:58






    • 3





      I gave you an answer. There is no backslash to remove.

      – 42-
      Jan 28 '15 at 23:59






    • 1





      I am passing this to string to SQL and it does see the "" that why i need to remove it.

      – user3022875
      Jan 29 '15 at 0:04






    • 1





      You are repeating anthropomorphic characterizations of programmatic behavior. Please provide the requested code and error messages.

      – 42-
      Jan 29 '15 at 0:12






    • 1





      Right. That's what cat is documented to return. You still have not provided any evidence that whatever instance of "SQL" is being communicated with is producing an error related tot he presence of a backslash in the test object.

      – 42-
      Jan 29 '15 at 0:22














    10












    10








    10







    Perhaps you would like to see a different representation of the same string:



    test2 <- 'select case when "est" dsaf'
    test<- "select case when "est" dsaf"
    identical(test, test2)
    #[1] TRUE


    When a character value is built with double quotes, any interior instances of " become only double-quotes. They will be displayed by print (and by the REPL that you see in an interactive session) with the escape-backslash, but using cat you cant determine that they are not really in there as backslashes.



    Further proof:



    > nchar(""")
    [1] 1


    You can use either cat or print with quote=FALSE in you want to display the value as it really exists internally:



    > print(test, quote=FALSE)
    [1] select case when "est" dsaf


    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:



    > require(sqldf)
    Loading required package: sqldf
    Loading required package: gsubfn
    Loading required package: proto
    Loading required package: RSQLite
    Loading required package: DBI
    > ?sqldf
    > a1r <- head(warpbreaks)
    > a1s <- sqldf("select * from warpbreaks limit 6")
    Loading required package: tcltk
    > a2s <- sqldf("select * from CO2 where Plant like 'Qn%'")
    >
    > a2sdq <- sqldf("select * from CO2 where Plant like "Qn%"")
    > identical(a2s,a2sdq)
    [1] TRUE





    share|improve this answer















    Perhaps you would like to see a different representation of the same string:



    test2 <- 'select case when "est" dsaf'
    test<- "select case when "est" dsaf"
    identical(test, test2)
    #[1] TRUE


    When a character value is built with double quotes, any interior instances of " become only double-quotes. They will be displayed by print (and by the REPL that you see in an interactive session) with the escape-backslash, but using cat you cant determine that they are not really in there as backslashes.



    Further proof:



    > nchar(""")
    [1] 1


    You can use either cat or print with quote=FALSE in you want to display the value as it really exists internally:



    > print(test, quote=FALSE)
    [1] select case when "est" dsaf


    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:



    > require(sqldf)
    Loading required package: sqldf
    Loading required package: gsubfn
    Loading required package: proto
    Loading required package: RSQLite
    Loading required package: DBI
    > ?sqldf
    > a1r <- head(warpbreaks)
    > a1s <- sqldf("select * from warpbreaks limit 6")
    Loading required package: tcltk
    > a2s <- sqldf("select * from CO2 where Plant like 'Qn%'")
    >
    > a2sdq <- sqldf("select * from CO2 where Plant like "Qn%"")
    > identical(a2s,a2sdq)
    [1] TRUE






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 29 '15 at 0:27

























    answered Jan 28 '15 at 23:48









    42-42-

    213k15261401




    213k15261401







    • 2





      Do you have an answer?

      – user3022875
      Jan 28 '15 at 23:58






    • 3





      I gave you an answer. There is no backslash to remove.

      – 42-
      Jan 28 '15 at 23:59






    • 1





      I am passing this to string to SQL and it does see the "" that why i need to remove it.

      – user3022875
      Jan 29 '15 at 0:04






    • 1





      You are repeating anthropomorphic characterizations of programmatic behavior. Please provide the requested code and error messages.

      – 42-
      Jan 29 '15 at 0:12






    • 1





      Right. That's what cat is documented to return. You still have not provided any evidence that whatever instance of "SQL" is being communicated with is producing an error related tot he presence of a backslash in the test object.

      – 42-
      Jan 29 '15 at 0:22













    • 2





      Do you have an answer?

      – user3022875
      Jan 28 '15 at 23:58






    • 3





      I gave you an answer. There is no backslash to remove.

      – 42-
      Jan 28 '15 at 23:59






    • 1





      I am passing this to string to SQL and it does see the "" that why i need to remove it.

      – user3022875
      Jan 29 '15 at 0:04






    • 1





      You are repeating anthropomorphic characterizations of programmatic behavior. Please provide the requested code and error messages.

      – 42-
      Jan 29 '15 at 0:12






    • 1





      Right. That's what cat is documented to return. You still have not provided any evidence that whatever instance of "SQL" is being communicated with is producing an error related tot he presence of a backslash in the test object.

      – 42-
      Jan 29 '15 at 0:22








    2




    2





    Do you have an answer?

    – user3022875
    Jan 28 '15 at 23:58





    Do you have an answer?

    – user3022875
    Jan 28 '15 at 23:58




    3




    3





    I gave you an answer. There is no backslash to remove.

    – 42-
    Jan 28 '15 at 23:59





    I gave you an answer. There is no backslash to remove.

    – 42-
    Jan 28 '15 at 23:59




    1




    1





    I am passing this to string to SQL and it does see the "" that why i need to remove it.

    – user3022875
    Jan 29 '15 at 0:04





    I am passing this to string to SQL and it does see the "" that why i need to remove it.

    – user3022875
    Jan 29 '15 at 0:04




    1




    1





    You are repeating anthropomorphic characterizations of programmatic behavior. Please provide the requested code and error messages.

    – 42-
    Jan 29 '15 at 0:12





    You are repeating anthropomorphic characterizations of programmatic behavior. Please provide the requested code and error messages.

    – 42-
    Jan 29 '15 at 0:12




    1




    1





    Right. That's what cat is documented to return. You still have not provided any evidence that whatever instance of "SQL" is being communicated with is producing an error related tot he presence of a backslash in the test object.

    – 42-
    Jan 29 '15 at 0:22






    Right. That's what cat is documented to return. You still have not provided any evidence that whatever instance of "SQL" is being communicated with is producing an error related tot he presence of a backslash in the test object.

    – 42-
    Jan 29 '15 at 0:22














    -1














    The solution in R - gsub replacing backslashes worked for me.



    Example:



    library(stringr)
    df$variable <- str_replace(df$variable,"\\","")

    df$variable before: "xyz"
    df$variable after:"xyz"





    share|improve this answer





























      -1














      The solution in R - gsub replacing backslashes worked for me.



      Example:



      library(stringr)
      df$variable <- str_replace(df$variable,"\\","")

      df$variable before: "xyz"
      df$variable after:"xyz"





      share|improve this answer



























        -1












        -1








        -1







        The solution in R - gsub replacing backslashes worked for me.



        Example:



        library(stringr)
        df$variable <- str_replace(df$variable,"\\","")

        df$variable before: "xyz"
        df$variable after:"xyz"





        share|improve this answer















        The solution in R - gsub replacing backslashes worked for me.



        Example:



        library(stringr)
        df$variable <- str_replace(df$variable,"\\","")

        df$variable before: "xyz"
        df$variable after:"xyz"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 17 '17 at 2:40

























        answered Jul 17 '17 at 1:39









        TedTed

        314




        314



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28204507%2fremove-backslashes-from-character-string%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto