SQL 如何在SQL“IN”子句中使用包含'的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15173773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:00:19  来源:igfitidea点击:

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

sqlselect

提问by Candy

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

我有一个像“Apple's”这样的字符串值。我想在 SQL "IN" 子句中使用这个字符串,就像下面的查询

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

how i can get the above query work correctly ?

我如何才能使上述查询正常工作?

Many Thanks, Awais Afzal.

非常感谢,Awais Afzal。

回答by John Woo

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 :)

但如果您在应用程序级别执行此操作,请确保您对查询进行了参数化 :)

回答by Giznary

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.

我发现 SQL 正确地将 ASCII 单引号(ALT 0146)解释为搜索中的撇号,而“IN”将其视为任何其他字符。现在我可以使用 Matt(ASCII 字符 0146)s Macintosh”搜索“Matt's Macintosh”,而不会弄乱我的列表或搜索。