五月 16, 2023
Oracle,当需要在字符串中包含单引号时,我们需要输入一对单引号。
例如:
SQL> select 'Hello, I''m John Doe.' from dual;
'HELLO,I''MJOHNDOE.
------------------
Hello, I'm John Doe.
使用q quote的写法,可以将quote内部的字符串原样输出,避免写多个单引号带来的困惑。
q'c text-to-be-quoted c' c is a single character (called the quote delimiter).
With the ?quote operator? apostrophes don't have to
be doubled:
SQL> select q'#Oracle's quote operator#' from dual;
Q'#ORACLE'SQUOTEOPERATO
-----------------------
Oracle's quote operator
SQL> select q'(Hello I'm John Doe.)' from dual;
Q'(HELLOI'MJOHNDOE
-----------------
Hello I'm John Doe.
PostgreSQL q quote
PostgreSQL 中有多种引用字符串的方法,详细说明,可以查看文档。
使用成对双$
即可,或者$tag$
成对。
例子:
postgres=# select 'Hello, I''m John Doe';
?column?
-------------------
Hello, I'm John Doe
(1 row)
postgres=# select $$Hello, I'm John Doe$$;
?column?
-------------------
Hello, I'm John Doe
(1 row)
postgres=# select $abc$Hello, I'm John Doe$abc$;
?column?
-------------------
Hello, I'm John Doe
(1 row)
更多PostgreSQL高级quote
关键字quote与字符串quote
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+----------------+------------------+---------------------+--------
pg_catalog | quote_ident | text | text | normal
pg_catalog | quote_literal | text | anyelement | normal
pg_catalog | quote_literal | text | text | normal
pg_catalog | quote_nullable | text | anyelement | normal
pg_catalog | quote_nullable | text | text | normal
(5 rows)
1、关键字(例如表名、字段、库名等对象名),自动封装双引号。
postgres=# select quote_ident('Tbl');
quote_ident
-------------
"Tbl"
(1 row)
2、字符串,自动封装单引号。(输入NULL,返回空)
postgres=# select quote_literal('hello, i''m John Doe');
quote_literal
----------------------
'hello, i''m John Doe'
(1 row)
postgres=# select quote_literal(null);
quote_literal
---------------
(1 row)
3、识别空字符串,并返回NULL字符串。
postgres=# select quote_nullable(null);
quote_nullable
----------------
NULL
(1 row)
postgres=# select quote_nullable('hello, i''m John Doe');
quote_nullable
----------------------
'hello, i''m John Doe'
(1 row)
格式化字符串
函数 format 根据一个格式字符串产生格式化的输出,其形式类似于 C 函数sprintf
。
format 常用于PLPGSQL,生成动态SQL。