SQL 中转义单引号的方法“LIKE”命令
在 SQL LIKE
命令中转义单引号 (') 有哪些不同的方法?
一种方法是每当您必须转义单引号时就添加两个单引号。
你们能提出一些建议吗?
数据库:SQL Server 2005 和 Oracle 10g
What are the various ways to ESCAPE single quotes(') in the SQL LIKE
command?
One way is to put two single quotes whenever you have to escape a single quote.
Can you people suggest something?
Databases: SQL Server 2005 and Oracle 10g
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你已经有了答案。您必须使用两个单引号:
You already have the answer. You have to use two single quotes:
两个单引号是最好的解决方案。
或者,您可以使用
CHAR(39)
来表示单引号字符。Two single quotes is the best solution.
Alternatively, you can use a
CHAR(39)
to represent a single quote character.最好的方法是将参数与 ADO 或 ADO.NET 绑定。
就像(使用 ADO.NET 的 C# 示例):
在 SQL Server 2005 中,您转义了如果不想绑定,则使用单引号 (') 和双单引号 (''):
The best way is to bind the parameter with ADO or ADO.NET.
Like (example in C# with ADO.NET):
In SQL Server 2005 you escape a single quote (') with a double single quote ('') if you do not want to bind:
如果您要搜索值Allen O'Dea,请执行以下查询。
这个查询工作完美。
If you want to search the value Allen O'Dea following is the query.
This query works perfectly.
还有“Q-quote”方法:
该方法自 Oracle 10.2 起可用。
我使用“#”字符作为引号分隔符,但您几乎可以使用不会出现在字符串中的任何字符(有一些例外,例如空格字符)。
在像上面这样的简单示例中,我可能不会这样做。我只是将单引号加倍,但在构建包含大量字符串文字的大型动态 SQL 语句时,它确实派上用场。
There is also the "Q-quote" method:
This is available since Oracle 10.2.
I used a '#' character as the quote delimiter, but you can use pretty much any character that won't appear in the string (there are a few exceptions, such as the space character).
In a simple example like that above I probably wouldn't do this. I'd just double up the single quotes, but it does come in handy when building large dynamic SQL statements that include lots of string literals.