问题描述
我有以下sql命令,我需要在PostgreSQL中转义括号,我该怎么做?
I have the following sql command, i need to escape parentheses in PostgreSQL, how can i do that?
SELECT rua
FROM logradouros
WHERE rua ~* 'Antonio De Sant\'Ana Galvao Av Frei (Av 01 Parte A)'
LIMIT 100;
推荐答案
使用反斜杠转义括号。请注意,如果 standard_conforming_strings 参数设置为off(这是PostgreSQL 9.1之前的默认行为),您需要使用两个反斜杠 \\
。
Use backslash to escape parentheses. Note that if standard_conforming_strings parameter is set to off (which is default behaviour up to PostgreSQL 9.1) you need use two backslashes \\
.
通常有三种方法来转义括号:
Generally there are three approaches how to escape parentheses:
- 普通
'pattern'
语法,具体取决于standard_conforming_strings设置 - 转义字符串常量,例如
E'pattern'
- 用美元报价的字符串常量
$$ pattern $$
或$ sometext $ pattern $ sometext $
- ordinary
'pattern'
syntax, which is dependent on standard_conforming_strings setting - escape string constant e.g.
E'pattern'
- dollar-quoted string constants
$$pattern$$
or$sometext$pattern$sometext$
第一个是标准SQL(尤其是带有standard_conforming_strings的SQL),其他是PostgreSQL扩展。选择任何喜欢的方法。
The first one is standard SQL (especially with standard_conforming_strings), others are PostgreSQL extensions. Choose whatever method you like.
这里有一些示例:
SET standard_conforming_strings = 0;
SELECT
'Antonio De Sant''Ana Galvao Av Frei (Av 01 Parte A)'
~*
'Antonio De Sant\'Ana Galvao Av Frei \\(Av 01 Parte A\\)'
SET standard_conforming_strings = 1;
SELECT
'Antonio De Sant''Ana Galvao Av Frei (Av 01 Parte A)'
~*
'Antonio De Sant''Ana Galvao Av Frei \(Av 01 Parte A\)'
SELECT
'Antonio De Sant''Ana Galvao Av Frei (Av 01 Parte A)'
~*
E'Antonio De Sant\'Ana Galvao Av Frei \\(Av 01 Parte A\\)'
SELECT
'Antonio De Sant''Ana Galvao Av Frei (Av 01 Parte A)'
~*
$$Antonio De Sant'Ana Galvao Av Frei \(Av 01 Parte A\)$$
SET standard_conforming_strings = default;
这篇关于SQL-如何在PostgreSQL中转义括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!