Results 1 to 6 of 6

Thread: Easy question

  1. #1
    Join Date
    Jun 2003
    Location
    Montreal
    Posts
    38

    Unanswered: Easy question

    Hi,

    I have a query that I need to pass as a string as the second parameter of the OpenQuery method. Here's my query:

    SELECT * FROM mytable WHERE last_name = 'DOE'

    Thing is that a string is set with apostrophies, so I don't know how to set my string since apostrophies are alse in my query. Normally, it would look like this:

    DECLARE @CQUERY VARCHAR(100)
    SET @CQUERY = 'SELECT * FROM mytable WHERE last_name = 'DOE''

    But of course this fails. How can I do it then?

    Thanks,

    Skip.

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    I always do it thid way


    Code:
    DECLARE @CQUERY VARCHAR(100)
    SET @CQUERY = 'SELECT * FROM mytable WHERE last_name = '+ '''' + 'DOE' + ''''
    SELECT @cquery
    Only cause it's easier for me to read......
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    SET @CQUERY = "SELECT * FROM mytable WHERE last_name = 'DOE'"
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Jun 2003
    Location
    Montreal
    Posts
    38
    What's the order of the characters (I can't see them correctly with these fonts)?

    Is it 2 double quotes, 3 apostrophies, etc.

    Thanks again,

    Skip

  5. #5
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    I do


    1 quote text message 1 quoye + 4 quotes + 1 qoute value 1 quote + 4 quotes...

    but you should be able to cut and paste the code into QA...
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  6. #6
    Join Date
    Jun 2003
    Location
    Montreal
    Posts
    38
    OK, I've tried a couple of solutions and I can see that something like this works fine:

    DECLARE @CQUERY VARCHAR(100)
    SET @CQUERY = 'SELECT * FROM mytable WHERE last_name = ' + '''' + 'DOE' + ''''

    Here, '''' = 4 apostrophies.

    Now, I find it wierd that this worked because shoudn't 4 apostrophies open-close empty strings twice (hence creating an error because the + sign is not between them)?

    Is this a special case programmed for text appending in SQL Server?

    Thanks,

    Skip.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •