SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Finding Apostrophes in String and Text – Day 3 of 35

Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 1.
Every day one winner from United States will get Joes 2 Pros Volume 1.

Finding Apostrophes in string and text

For the last two days we have been using wildcard examples from the Beginning SQL Joes 2 Pros Volume 1 book. Today is our last wildcard example. Please take one more look all the records in the Grant table of the JProCo database. Notice GrantID 004 and 005 have a single quote (apostrophe) in the name. See figure below:

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Finding Apostrophes in String and Text - Day 3 of 35 j2p_3_1

What if you want to find grants that have an apostrophe (single quote) in their names such as Norman’s Outreach? Everything inside single quotes after the LIKE evaluates every record to give you your final result set.

The first single quote starts the string and it ends with the second single quote. Everything between the single quotes is part of the search string. Everything before the first single quote and after the second single quote is not part of the search string. The single quote encompasses or delimits the pattern you are searching. A new challenge arises here. The following query produces a syntax error.

--Bad query results in an error.
SELECT *
FROM [GRANT]
WHERE GrantName LIKE '%'%'

'Msg 105, Level 15, State 1, Line 4
Unclosed quotation mark after the character string ‘

The problem lies in the fact that SQL Server assumes the predicate is done after the second single quote. SQL Server sees everything after that second single quote as an error in your SQL code. Your intentions were lost or misunderstood. To forego the special meaning of the single quote, precede it with another single quote. The code and results are seen in the figure below.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Finding Apostrophes in String and Text - Day 3 of 35 j2p_3_2

Using two single quotes filters your result set for a grant name with an apostrophe. You now have two records with a single quote in your result set. To view all names without a single quote you would simply change the LIKE to NOT LIKE in the WHERE clause.

--Find GrantNames without a single quote
SELECT *
FROM [GRANT]
WHERE GrantName NOT LIKE '%''%'


Note: If you want to setup the sample JProCo database on your system you can watch this video.

Question 3:

Q 3:You want to find all first names that have an apostrophe anywhere in the name. Which SQL code would you use?

  1. SELECT * FROM Employee
    WHERE Firstname like ‘_’% ‘
  2. SELECT * FROM Employee
    WHERE Firstname like ‘_”% ‘
  3. SELECT * FROM Employee
    WHERE Firstname like ‘_[‘]% ‘
  4. SELECT * FROM Employee
    WHERE Firstname like ‘%’% ‘
  5. SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘
  6. SELECT * FROM Employee
    WHERE Firstname like ‘%[‘]% ‘

Please post your answer in comment section to win Joes 2 Pros books.

Rules:

Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 1.
Winner from India will get Joes 2 Pros Volume 1.
The contest is open till next blog post shows up at which is next day GTM+2.5.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Tips from the SQL Development Series – Wildcard – Querying Special Characters – Day 2 of 35
Next Post
SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Efficient Query Writing Strategy – Day 4 of 35

Related Posts

191 Comments. Leave new

  • Correct Answer is: Option 5 :
    SELECT * FROM Employee
    WHERE Firstname like ‘%”%’

    Explanation: We can find all first names that have an apostrophe anywhere in the name by using two single quotes, it filters our result set for all the first name with an apostrophe.

    Country: INDIA[Noida]

    Thanks,
    Dips

    Reply
  • Option 5 is correct.

    SELECT * FROM Employee WHERE FIRSTNAME LIKE ‘%”%’

    INDIA

    Reply
  • Correct Answer:

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Pratik Raval
    India

    Reply
  • The correct answer is option 5

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Here we want to find all first names that have an apostrophe anywhere in the name so we have to put % at start and end so character anywhere in string can be found to find apostrophe we need two single quotes (”) in between % sign.

    I am From
    INDIA

    Reply
  • Abhimanyu Kumar Vatsa
    August 3, 2011 10:21 am

    I will go with option 5

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Explanation – wildcard character ” = ‘
    If you are wishing to use “abhimanyu’s” then use “abhimanyu”s” and this will treated as “abhimanyu’s”.

    Country: India (Bokaro Steel City)

    Reply
  • Question #5 is correct.
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Timmy
    TX, USA

    Reply
  • Right answer is option 5

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Explanation :
    For finding all first names that have an apostrophe anywhere in the name
    you have to used this % sign followed by apostrophe again followed by apostrophe. This will search all names like all combinations of Alphabets having apostrophe anywhere in string.

    Thanks & Regards
    Sunny B. Jagtap

    From INDIA [Pune]

    Reply
  • Ashish Gilhotra
    August 3, 2011 10:48 am

    I think it is answer 5 you explained it in post itself

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Reply
  • Answer is option 5

    Reply
  • Option-5

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Reply
  • The Correct Answer is 5
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Narendra (India)

    Reply
  • As you have already mentioned in the post,
    To find all the names having an apostophe will require two ” for sql server to understand and making two % signs on both side will result all the names having an apostrophe in any position..

    Hence, answer 5 is correct.

    Ishan Shah,
    Gandhinagar,
    India

    Reply
  • correct answer option 5

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Thanks
    Kirti Darji

    Reply
  • Correct Option is 5
    means

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    because we have to prefix the single quote by another single quote to suppress its special meaning

    Thanks,
    Santosh
    India

    Reply
  • Mandar Alawani
    August 3, 2011 11:49 am

    Correct answer is :

    5.SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Mandar,
    Mumbai, INDIA

    Reply
  • Vivek Srivastava
    August 3, 2011 12:04 pm

    Option 5 is correct one:
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Thanks
    Vivek Srivastava

    Reply
  • 5. SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    sql server takes two single quotes as a single so we have to give two singe quotes.

    ghanshyam
    bangalore

    Reply
  • Correct option is 5

    Genarally ‘%%’ is use to match any word or character at any position and %”% will match single quote at any position either first or last or middle.

    karan
    India

    Reply
  • Rahul Singh rathore
    August 3, 2011 12:26 pm

    OPTION 5 is Correct answer

    Country : India

    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Reply
  • Nikhil Mahajan
    August 3, 2011 12:30 pm

    the correct option is option 5 ie
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    because using two single quote will help you to find string with apostrophe anywhere in string..

    i am from india

    Reply

Leave a Reply