Single Quote In Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    Single Quote In Variable

    I have a stored procedure containing the following,

    SELECT @cntctownerkey = (SELECT CustKey FROM tarCustomer WHERE CustName = @account)

    However, it does not return a value because value of @account contains a single quote. How can I handle this given I can't escape the quote because I do not know if/when one will occur in @account?
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by mcfly1204
    I have a stored procedure containing the following,

    SELECT @cntctownerkey = (SELECT CustKey FROM tarCustomer WHERE CustName = @account)

    However, it does not return a value because value of @account contains a single quote. How can I handle this given I can't escape the quote because I do not know if/when one will occur in @account?

    What's your CustName look like? Does it has the quote? If you need to remove the quote from the value of @account, just use the REPLACE function.

    -- CK

    Comment

    • mcfly1204
      New Member
      • Jul 2007
      • 233

      #3
      Originally posted by ck9663
      What's your CustName look like? Does it has the quote? If you need to remove the quote from the value of @account, just use the REPLACE function.

      -- CK
      @account should exactly match CustName. This being the case, I would have to use the REPLACE function for both @account and CustName then.

      Comment

      • mcfly1204
        New Member
        • Jul 2007
        • 233

        #4
        Originally posted by ck9663
        What's your CustName look like? Does it has the quote? If you need to remove the quote from the value of @account, just use the REPLACE function.

        -- CK
        So, I changed the query to,

        SELECT @account = REPLACE(@accoun t,"'"," ")

        SELECT @cntctownerkey = (SELECT CustKey FROM tarCustomer WHERE REPLACE(CustNam e, "'"," ") = @account)

        however that does not seem to be right. Any thoughts on how I can replace the apostrophe in both the selection from the column CustName as well as @account?

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          Paste this on your query analyzer:

          Code:
          declare @name varchar(50)
          
          
          set @name = 'clark''kent'
          
          select @name, replace(@name,'''','')

          Notice that the quote disappeared. On the replace function, I use 4 quotes in the second argument.

          Happy coding.

          -- CK

          Comment

          Working...