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

  • Rajneesh Verma
    August 3, 2011 5:14 pm

    Answer 5 is correct as we need to find out anywhere in the name
    Correct Answer – 5
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Thanks…
    Rajneesh Verma
    INDIA

    Reply
  • Mike Michalicek
    August 3, 2011 5:22 pm

    The answer is #5

    USA

    Mike Michalicek

    Reply
  • Correct Answer is 5,
    Country: UK
    City: Oxford

    Reply
  • Correct answer is

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

    Country : India
    Satish Singh

    Reply
  • Vishal S Jindal
    August 3, 2011 5:47 pm

    Correct Answer is:

    Option 5 :

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

    option 1,2,3 are directly negated because of wild character
    option 4 will generate error because the query results in undisclosed quotation mark
    option 5 : this is the right one
    the first ‘ after % works as a ‘escape’ for the following ‘
    option 6 : it will generate syntex error as query is suppose to end when second apsotrophe encounters.

    country : india

    Reply
  • Option 5.

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

    Is correct answer.

    I am from India

    Reply
  • Correct Answer: 5

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

    Vishal (India)

    Reply
  • Devendra Singh Rathore
    August 3, 2011 6:09 pm

    Correct answer is option 5

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

    Reply
  • aashishthakker
    August 3, 2011 6:38 pm

    Correct Answer: Option 5

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

    -Aashish Thakker (USA)

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

    Chetan (USA)

    Reply
  • S Saravanakumar
    August 3, 2011 6:46 pm

    Option 5 is correct answer – As per the above theory 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.

    If the character string contains an embedded single quotation mark, you should insert an additional single quotation mark in front of the embedded mark

    Saravanakumar. S
    Country – India

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

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

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

    Charlotte NC,USA

    Reply
  • Lisa Guiffrida
    August 3, 2011 7:57 pm

    Correct answer is 5 SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Lisa
    USA

    Reply
  • MOHAMMED ZUHEB
    August 3, 2011 8:22 pm

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

    MOHAMMED ZUHEB

    INDIA

    Reply
  • kalyanasundaram.K
    August 3, 2011 8:48 pm

    5th is the Answer

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

    its similar like insert a Firstname with an apostrophe (single quote)

    insert into Employee (Firstname) values (‘Micheal”s’)

    Reply
  • Answer: Option 5
    Because here the predicate starts and ends with % which means that there could be any character before and after apostrophes and there are two apostrophes which means that the 1st apostrophes is negating the next one and telling SQL Server to treat 2nd apostrophes as a part of string.

    Country:India

    Reply
  • Hi Pinal,

    Challenge:
    To find all first names that have an apostrophe anywhere in the name.

    Solution:
    To find all first names that have an apostrophe anywhere in the name, use the following query (5th choice of the six listed):

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

    Explanation:
    The Percent Wildcard (%) at the beginning gives all rows where name has zero or more characters before encountering the first apostrophe.

    The two single apostrophes together says search for an apostrophe in the name.

    The first apostrophe is signalling SQL Server to look for the next character explicitly in the string.

    The second apostrophe is the character to look for.

    The Percent Wildcard (%) at the end gives all rows where name has zero or more characters after encountering the apostrophe.

    Country of residence:
    United States

    Thanks for the knowledge!

    Regards,

    Bill Pepping

    Reply
  • Option 5 :)
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    USA

    Reply

Leave a Reply