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

  • Vaibhav Mathur
    August 4, 2011 12:20 am

    Should be –> SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Vaibhav Mathur
    India

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

    Reply
  • dilipkumarjena
    August 4, 2011 12:26 am

    Hi Pinal Sir,

    The correct answer for the above question is Option No. 5.

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

    Explanation

    We need to find Persons name that have an apostrophe anywhere in the name ,so it can be anywhere from starting till the end anywhere.

    1) In above option LIke operator suggests that we have some criteria and what all satisfy will be selected.
    2) % operator : it can be 0 to many characters from start till end.
    3) ” two single quotes tells the sql server that we need at least one apostrophe in the name.
    4) % operator : it can be 0 to many characters from start till end.

    NOTE 1) Make Sure that there is no space anywhere in the searching formula otherwise sql will look for apostrophe and space specified before/ after as specified then the answer or the result set will be surprising.

    The remaining options are in correct because:

    Option 1: SELECT * FROM Employee WHERE Firstname like ‘_’% ‘
    Unclosed quotation mark after the character string,Incorrect syntax near ‘ .

    Option 2: SELECT * FROM Employee WHERE Firstname like ‘_”% ‘
    Underscore is used instead of %, No results are returned wrong Query.

    Option 3: SELECT * FROM Employee WHERE Firstname like ‘_[‘]% ‘
    Incorrect syntax near ‘]’, Incorrect pattern we are searching the apostrophe anywhere in the string not only from second and also syntax error.

    Option 4: SELECT * FROM Employee WHERE Firstname like ‘%’% ‘
    Unclosed quotation mark after the character string, error raised Wrong Option.

    Option 6: SELECT * FROM Employee WHERE Firstname like ‘%[‘]% ‘
    Incorrect syntax near ‘]’, Query not formed properly.

    Hope to see more challenging one in next post, this is a good initiative taken by Pinal sir where in all are learning something new and useful I am happy that I am a part of this.

    Dilip Kumar Jena – INDIA

    Reply
  • SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘
    kkmishra (jss academy of technical education noida)
    India

    Reply
  • Correct answer is option 5

    SELECT * FROM Employee
    WHERE Firstname like ‘%”%

    Manoj ,New Delhi

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

    SQL Server sees everything after that second single quote as an error in SQL code. To forego the special meaning of the single quote, precede it with another single quote.

    USA

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

    Reply
  • The correct answer is option 5
    SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘

    Nikhil from Tampa, FL, USA

    Reply
  • answer i s option 5

    Good Post,

    sandhya
    Boston,usa

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

    I’m from USA

    Reply
  • Gerardo Melendrez (@payini)
    August 4, 2011 8:11 am

    Answer 5: SELECT * FROM Employee
    WHERE Firstname like ‘%”% ‘
    Residence: USA

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

    Redmond, WA [USA]

    Reply
  • Number 5 would be the correct answer.
    Jason
    USA

    Reply
  • Saurabh Sontakke
    August 4, 2011 9:09 am

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

    Reply
  • Yup, Number 5, I’ve had to do that enough with Dynamic SQL. Texas, USA.

    Reply
  • Dhruval Vikram Shah
    August 4, 2011 10:45 am

    Correct Answer is : Option 5

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

    Thanks,
    Dhruval Shah, Ahmadabad, India.

    Reply
  • Answer : Option 5

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

    Thanks
    Riyas.V.K

    Reply
  • Santosh Maurya
    August 4, 2011 11:02 am

    Option 5 Is correct

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

    Reply
  • Vanthiya Thevan. N
    August 4, 2011 11:16 am

    Correct Answer is Option 5:

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

    India

    Reply
  • Correct answer is option 5

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

    Thanks
    Rupesh Tiwary(India)

    Reply

Leave a Reply