21、Oracle:字符串中包含单引号怎么到数据库中查询

直接查询会报:ora:00907缺失右括号的异常

1.可以使用转义的方式,匹配:you are' beautiful可以使用:'you are'' beautiful'的方式

2.可以使用替换字符的方式:

sql = @"select * from table1 where replace(remark, '''', '‘')  in ('{0}')"

这里将数据库中的remark中的所有单引号换成中文的左单引号(此时外部匹配的字符串也要有这样的转换,可以使用正则函数)

例如:customername = regex.raplace(customername, "[']", "‘"),然后将customername丢到sql的in中去

3.第三种方法参见下文,常用的可以用q'[you are'beautiful]'

下文来自:https://www.2cto.com/database/201307/229728.html

1.关于在字符串中包含单引号

字符串是引用字符串的,如果字符串本身就包含单引号怎么办?

用2个单引号表示或者以q或Q开头,然后后面是字符串,字符串的第1个字符和最后结束的字符是分割符号,中间的引号随便写,如果开始是[,<,(,{结束一定要是],>,),}。但是开始如果是],>,),}那么结束要和开始相同,其他的符号,+,|,或字母a,A,等,开始和结束相同,包括大小写。

例子如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
SQL> select 'you are' ' beautiful' from dual;
 
'YOUARE' 'BEAUTIFUL
------------------
you are' beautiful
 
select q '[you are' beautiful] ' from dual;
 
 
Q' [YOUARE 'BEAUTIFU
----------------------
you are' beautiful
 
SQL> select q '+it' s a cat+ ' from dual
   2  /
 
Q' +IT 'SACAT+'
-------------
it 's a cat
 
 
SQL> select Q' Ait 's a cata' from dual
   2  /
 
select Q 'Ait' s a cata' from dual

猜你喜欢

转载自blog.csdn.net/xushaozhang/article/details/78437207
今日推荐