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

  • At first glance I would agree with Ritesh on the answer (option 5) and the explanation for not selecting the other options.
    However, there seem to be an extra space in the option 5 after the second wildcard:
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    I presume it is supposed to be:
    SELECT * FROM Employee
    WHERE Firstname like ‘%”%’

    As explained in the link from MSDN https://docs.microsoft.com/en-us/sql/t-sql/language-elements/percent-character-wildcard-character-s-to-match-transact-sql?view=sql-server-2017
    Wildcard – Matches any string of zero or more characters. This wildcard character can be used as either a prefix or a suffix.
    A single quote would return the unclosed quotation mark error therefore you require double quotes.
    Thanks,
    Shivani (USA)

    Reply
  • 5 is the correct answer. You have to escape the first apostrophe by doubling them up.

    Eric (USA)

    Reply
  • Answer = 5

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

    Country = USA

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

    Reply
  • Option 5 is the correct answers
    SELECT * FROM Employee
    WHERE Firstname like ‘%”%‘

    This will search the Firstname column which has atleast 1 apostrophe. Other queries will error out and not return any results

    Reply
  • Option 5 is closest, but there again, you have a blank char at the end of each item which would cause an issue for all of them.
    Corrected option 5:
    SELECT * FROM Employee
    WHERE Firstname like ‘%”%‘

    Reply
  • Option 5 is closest, but there again, you have a blank char at the end of each item which would cause an issue for all of them.
    Corrected option 5:
    SELECT * FROM Employee
    WHERE Firstname like ‘%”%‘

    I forgot the country again! USA

    Reply
  • The correct anser is :

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

    Country: United States

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

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

    You need the”%” at the start and end so that any number of characters can be before or after the apostrophe, while the two single quotes between the “%” signs indicate that the desired character is an apostrophe.

    USA

    Reply
  • Simhadri Basava
    August 3, 2011 11:27 pm

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

    Reply
  • Shripati Hegde
    August 3, 2011 11:30 pm

    Answer is 5

    SELECT * FROM Employee
    WHERE LastName like ‘%”%’

    Thanks
    Shree

    Reply
  • Dhanu Dhanapalan
    August 3, 2011 11:35 pm

    Option 5 would work fine.

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

    Dhanu Dhanapalan(India)

    Reply
  • Santhosh Kumar
    August 3, 2011 11:35 pm

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

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

    Vinay,Pune

    Reply
  • Soumya Ranjan Das
    August 3, 2011 11:44 pm

    Option 5 is the correct answer.
    5. SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    All the other queries will return error messages

    SOumya Ranjan Das
    India

    Reply
  • When I ran #2, it did find apostrophes, however, it did not catch all of the apostrophes. #5 caught all of the apostrophes, but, does have an extra space on the end.

    Reply
  • Kaushik Thanki
    August 3, 2011 11:53 pm

    the ans is 6

    Reply
  • Chaitanya S Patil
    August 4, 2011 12:03 am

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

    Because this record has the single quote in (firstname) result set.
    INDIA (Bangalore)

    Reply
  • Arul Prakash. A
    August 4, 2011 12:04 am

    Correct answer : option 5

    Option : 1, 3, 4 and 6 -> Syntax error
    Option : 2 -> It will fetch the name which have Apostrophe exactly in the second position.
    Option : 5-> It will fetch the first names that have an apostrophe anywhere in the name

    Country : USA (Chicago)

    Reply

Leave a Reply