Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use a string that contain ' in SQL "IN" clause

Tags:

i have a string value like 'Apple's'. i want to use this string in SQL "IN" clause like below query

select * from tbl_fruit where nm_fruit IN(''Apple's'','Orange'); 

how i can get the above query work correctly ?

Many Thanks, Awais Afzal.

like image 751
Candy Avatar asked Mar 02 '13 11:03

Candy


People also ask

How do you check if a word contains in a string in SQL?

Method 1 - Using CHARINDEX() function This function is used to search for a specific word or a substring in an overall string and returns its starting position of match. In case no word is found, then it will return 0 (zero).

What is %% in SQL query?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters.

Can we use contains in SQL?

CONTAINS is a predicate used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server full-text search on full-text indexed columns containing character-based data types. CONTAINS can search for: A word or phrase. The prefix of a word or phrase.

How do you check if a string contains a substring SQL?

We can use the CHARINDEX() function to check whether a String contains a Substring in it. Name of this function is little confusing as name sounds something to do with character, but it basically returns the starting position of matched Substring in the main String.


2 Answers

double the single quotes,

select * from tbl_fruit where nm_fruit IN ('Apple''s', 'Orange') 

but if you do it on the application level, make sure you parameterized the query :)

like image 95
John Woo Avatar answered Sep 27 '22 21:09

John Woo


I have found SQL correctly interprets the ASCII single-closed-quote (ALT 0146) as an apostrophe in searches while the "IN" treats it like any other character. Now I can search for 'Matt's Macintosh' using Matt(ASCII character 0146)s Macintosh" without messing up my list or the search.

like image 40
Giznary Avatar answered Sep 27 '22 20:09

Giznary