处理 DB2 中的转义序列字符

发布于 2024-12-11 22:06:55 字数 161 浏览 0 评论 0原文

我想搜索一列并获取值包含 \ 的值。

我尝试从“Values”中选择*,其中“ValueName”如'\'。但没有返回任何值。

还尝试了 like "\"like'\''%' 等,但没有结果。

I want to search a column and get values where value containts \ .

I tried select * from "Values" where "ValueName" like '\'. But returns no value.

Also tried like "\" and like'\''%' etc. But no results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

九八野马 2024-12-18 22:06:55

请参阅 LIKE 谓词,特别是有关转义表达式的部分。

你想要的是

select * from Values where ValueName like '\\%' escape '\'

给出一个用法的例子:

create table backslash_escape_test
(
backslash_escape_test_column varchar(20)
);

insert into backslash_escape_test(backslash_escape_test_column) 
values ('foo\');
insert into backslash_escape_test(backslash_escape_test_column) 
values ('no slashes here');
insert into backslash_escape_test(backslash_escape_test_column) 
values ('foo\bar');
insert into backslash_escape_test(backslash_escape_test_column)
values ('\bar');

select count(*) from backslash_escape_test where 
backslash_escape_test_column like '%\\%' escape '\';

返回3(所有3行都带有\)。

select count(*) from backslash_escape_test where 
backslash_escape_test_column like '\\%' escape '\';

返回 1(\bar 行)。

See the DB2 Documentation on the LIKE predicate, in particular the parts about escape expressions.

What you want is

select * from Values where ValueName like '\\%' escape '\'

To give an example of usage:

create table backslash_escape_test
(
backslash_escape_test_column varchar(20)
);

insert into backslash_escape_test(backslash_escape_test_column) 
values ('foo\');
insert into backslash_escape_test(backslash_escape_test_column) 
values ('no slashes here');
insert into backslash_escape_test(backslash_escape_test_column) 
values ('foo\bar');
insert into backslash_escape_test(backslash_escape_test_column)
values ('\bar');

select count(*) from backslash_escape_test where 
backslash_escape_test_column like '%\\%' escape '\';

returns 3 (all 3 rows with \ in them).

select count(*) from backslash_escape_test where 
backslash_escape_test_column like '\\%' escape '\';

returns 1 (the \bar row).

倾`听者〃 2024-12-18 22:06:55
select * from Values where ValueName like '%\\%'

value 是一个不太好的名称,因为它可能与 value 关键字混淆

select * from Values where ValueName like '%\\%'

values is a not so good name because it may be confused with the values keyword

静谧幽蓝 2024-12-18 22:06:55

不要逃避它。您只需要在它周围使用通配符,如下所示:

select count(*)
  from escape_test
  where test_column like '%\%'

但是,假设您确实需要转义斜线。这是一个更简单、更直接的答案:

转义表达式允许您指定您想要的任何转义字符。那么为什么要使用您正在寻找的角色,从而要求您转义它呢?请改用任何其他字符。我将使用加号作为示例,但它可以是反斜杠、井号、问号、除您要查找的字符或通配符之一(% 或 _)之外的任何字符。

select count(*) 
  from escape_test
  where test_column like '%\%' escape '+';

现在您不必在您的类似模式中添加任何内容。

为了让自己遵守@Michael 所演示的相同证明标准——

create table escape_test
( test_column varchar(20) );

insert into escape_test 
         (test_column) 
  values ('foo\'),
         ('no slashes here'),
         ('foo\bar'),
         ('\bar');

select 'test1' trial, count(*) result
  from escape_test
  where test_column like '%\%' 
UNION
select 'test2', count(*)
  from escape_test
  where test_column like '%\\%' escape '\'
UNION
select 'test3', count(*)
  from escape_test
  where test_column like '%\%' escape '+'
;

每个方法返回相同数量的行:

TRIAL RESULT
----- ------
test1   3
test2   3
test3   3

Don't escape it. You just need wildcards around it like this:

select count(*)
  from escape_test
  where test_column like '%\%'

But, suppose you really do need to escape the slash. Here's a simpler, more straightforward answer:

The escape-expression allows you to specify whatever character for escaping that you wish. So why use a character that you're looking for, thus requiring you to escape it? Use any other character instead. I'll use a plus sign as an example, but it could be a backslash, pound-sign, question-mark, anything other than a character you are looking for or one of the wildcard characters (% or _).

select count(*) 
  from escape_test
  where test_column like '%\%' escape '+';

Now you don't have to add anything into your like-pattern.

To hold myself to the same standard of proof that @Michael demonstrated --

create table escape_test
( test_column varchar(20) );

insert into escape_test 
         (test_column) 
  values ('foo\'),
         ('no slashes here'),
         ('foo\bar'),
         ('\bar');

select 'test1' trial, count(*) result
  from escape_test
  where test_column like '%\%' 
UNION
select 'test2', count(*)
  from escape_test
  where test_column like '%\\%' escape '\'
UNION
select 'test3', count(*)
  from escape_test
  where test_column like '%\%' escape '+'
;

Which returns the same number of rows for each method:

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