SQL 中转义单引号的方法“LIKE”命令

发布于 2024-09-14 21:25:19 字数 245 浏览 7 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

把梦留给海 2024-09-21 21:25:19

你已经有了答案。您必须使用两个单引号:

select * from table where field like '%''something''%'

You already have the answer. You have to use two single quotes:

select * from table where field like '%''something''%'
作业与我同在 2024-09-21 21:25:19

两个单引号是最好的解决方案。

或者,您可以使用 CHAR(39) 来表示单引号字符。

UPDATE Employee SET LastName = 'O' +  CHAR(39) + 'Brien' 
WHERE ID=1;

Two single quotes is the best solution.

Alternatively, you can use a CHAR(39) to represent a single quote character.

UPDATE Employee SET LastName = 'O' +  CHAR(39) + 'Brien' 
WHERE ID=1;
素衣风尘叹 2024-09-21 21:25:19

最好的方法是将参数与 ADO 或 ADO.NET 绑定。

就像(使用 ADO.NET 的 C# 示例):

SqlCommand x = new SqlCommand(sqlConnection, @"select * from table where col like '%'+@para1+'%'");
x.parameters.add(new SqlParameter("@para1",sqltype.varchar,100)).value = "this is a' test";

SQL Server 2005 中,您转义了如果不想绑定,则使用单引号 (') 和双单引号 (''):

select * from table where col like '%this is a'' test%'

The best way is to bind the parameter with ADO or ADO.NET.

Like (example in C# with ADO.NET):

SqlCommand x = new SqlCommand(sqlConnection, @"select * from table where col like '%'+@para1+'%'");
x.parameters.add(new SqlParameter("@para1",sqltype.varchar,100)).value = "this is a' test";

In SQL Server 2005 you escape a single quote (') with a double single quote ('') if you do not want to bind:

select * from table where col like '%this is a'' test%'
蒲公英的约定 2024-09-21 21:25:19

如果您要搜索值Allen O'Dea,请执行以下查询。

SELECT * FROM [TABLE] WHERE [COLUMN] LIKE '%Allen O''Dea%'

这个查询工作完美。

If you want to search the value Allen O'Dea following is the query.

SELECT * FROM [TABLE] WHERE [COLUMN] LIKE '%Allen O''Dea%'

This query works perfectly.

无敌元气妹 2024-09-21 21:25:19

还有“Q-quote”方法:

select * from mytable where text like q'#%Here's some text%#';

该方法自 Oracle 10.2 起可用。

我使用“#”字符作为引号分隔符,但您几乎可以使用不会出现在字符串中的任何字符(有一些例外,例如空格字符)。

在像上面这样的简单示例中,我可能不会这样做。我只是将单引号加倍,但在构建包含大量字符串文字的大型动态 SQL 语句时,它确实派上用场。

There is also the "Q-quote" method:

select * from mytable where text like q'#%Here's some text%#';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文