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:
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.
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?
- SELECT * FROM Employee
WHERE Firstname like ‘_’% ‘ - SELECT * FROM Employee
WHERE Firstname like ‘_”% ‘ - SELECT * FROM Employee
WHERE Firstname like ‘_[‘]% ‘ - SELECT * FROM Employee
WHERE Firstname like ‘%’% ‘ - SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘ - 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)
191 Comments. Leave new
Correct answer is option 5
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Thanks
Dhananjay Kumar
Answer is -5
5.SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Neelesh Jain
India
Correcto answer is No. 5
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Correct answer is No. 5
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Rene Castro (El Salvador)
answer is 5
using two single quotes in the string let sql server recognise it as single quote.
Australia
Answer is option 5:
5.SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
because option 5 has double apostrophes to look for string literal
Country – India
Thanks.
Correct Answer: 5
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Thanks,
Bindish Thakkar
USA
5.
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
% is for any (or no) characters
‘ is the escape sequence for ‘
Leo Pius
USA
Answer is Option 5.
This query will return employees with apostrophe anywhere in the firstname.
Why other options are wrong?
Option 1 & 4 will end up with “Unclosed quotation mark ” error
Option 3 & 6 will end up with “Incorrect syntax near ‘]'” error
Option 2 – will yield Employee records who’s FristName’s second character is *Apostrophe*, which not our requirement.
Sandeep
INDIA
Answer is 5
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Rahul
Hyderabad
Correct Answer is option 5
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Thanks
Prasad Yangamuni
India (Pune)
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?
Answer is 5
5. SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Because As mentioned in earlier posts Wild cards works only with LIKE keyword and Now let us look at the where clause condition ‘%”%’
Which represents as follows
% —–> Represents Zero or more characters
” —–> To use Single quote as filter character, two continuous single quotes
should be mentioned so that to nullify its meaning
% character is specified before and after the two single quotes, Because we are looking for the single quote anywhere in the FirstName
Thanks for the Post :-)
Country: India
Option 5 would work fine.
SELECT * FROM Employee
WHERE Firstname like ‘%”%’
— after like there is one single quote, than percentage sign, after that TWO SINGLE QUOTE (not one double quote) than again percentage and single quote.
Ritesh Shah (India)
Correct answer is option 5
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
we are finding apostrophe anywhere in the string, that’s why we put another apostrophe precede the main one.
India
Answer is
5.SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Country :India [Ahmedabad]
Option 5 :
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
is the correct answer.
India
Correct Answer is : 5
5. SELECT * FROM Employee WHERE Firstname Like ‘%”%’
Gopalakrishnan Arthanarisamy
India
To find all the names that have an apostrophe anywhere in the firstname from the employee table, we should use LIKE and option 5 is the correct answer.
SELECT * FROM Employee
WHERE FirstName like ‘%”%’
From USA.
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
SELECT * FROM Employee
WHERE Firstname like ‘%”% ‘
Aditya
Chennai, INDIA