How to handle apostrophes

  • Update myTable

    Set strwhere = 'C.IT_Code IN ('A','E','C','P','R','O') AND SP.Status='S' and SP.PayDT <> '********' AND (NOT (C.ContractStatus IN ('D', 'X')))'

    where

    ReportID = 800

    The above sql statement gives an error because of the apostrophe. How do I handle the apostrophes so that the update statement runs correctly?

    thanks

  • Any idea?

  • You escape quotes by doubling them.

    Set strwhere = 'C.IT_Code IN (''A'',''E'',''C'',''P'',''R'',''O'') AND SP.Status=''S'' and SP.PayDT <> ''********'' AND (NOT (C.ContractStatus IN (''D'', ''X'')))'

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • My question would be why are you using dynamic SQL? That is why you are having a problem with quotes and Gail has given you the solution for that, if dynamic SQL is required.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Hi Jeffrey

    it looks to me he is storing all these different queries/conditions in a table.

    Its not a good idea. What it is your actual requirement??

  • Actually the Gail's response worked.

    Thank you

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply