PL/SQL中的字符串实际上是一个具有可选大小规格的字符序列。字符可以是数字,字母,空白,特殊字符或全部的组合。 PL/SQL提供三种字符串 -
- 固定长度字符串 - 在这样的字符串中,程序员在声明字符串时指定长度。该字符串的右边填充规定的长度。
- 可变长度字符串 - 在这样的字符串中,指定字符串的最大长度达
32,767
,并且不会填充。 - 字符大对象(CLOB) - 这些可变长度字符串最多可达
128TB
。
PL/SQL字符串可以是变量或文字。 字符串文字用引号括起来。 例如,
'This is a string literal.'
--或者
'hello world'
要在字符串文字中包含单引号,需要在彼此之间键入两个单引号。 例如,
'this isn''t what it looks like'
声明字符串变量
Oracle数据库提供了许多字符串数据类型,如:CHAR
,NCHAR
,VARCHAR2
,NVARCHAR2
,CLOB
和NCLOB
。 以“N”
为前缀的数据类型为“国家字符集”数据类型,用于存储Unicode字符数据。
如果需要声明一个可变长度的字符串,则必须提供该字符串的最大长度。例如,VARCHAR2
数据类型。 以下示例说明声明和使用一些字符串变量 -
SQL> declare
2 name varchar2(20);
3 company varchar2(30);
4 introduction clob;
5 choice char(1);
6 begin
7 name := 'ageeklet';
8 company := 'deli';
9 introduction := 'Hello, I''m ageeklet from deli.';
10 choice := 'y';
11 if choice = 'y' then
12 dbms_output.put_line(name);
13 dbms_output.put_line(company);
14 dbms_output.put_line(introduction);
15 end if;
16 end;
17 /
ageeklet
ageeklet
Hello, I'm ageeklet from deli.
PL/SQL procedure successfully completed
要声明一个固定长度的字符串,请使用CHAR
数据类型。 在这里,不必为固定长度变量指定最大长度。 如果不考虑长度约束,Oracle数据库将自动使用所需的最大长度。以下两个声明是相同的 -
red_flag CHAR(1) := 'Y';
red_flag CHAR := 'Y';
PL/SQL字符串函数和运算符
PL/SQL提供用于连接两个字符串的级联运算符(||
)。 下表提供了PL/SQL提供的字符串函数 -
编号 | 函数 | 描述 |
---|---|---|
1 | ASCII(x); | 返回字符x 的ASCII值。 |
2 | CHR(x); | 返回ASCII值为x 的字符。 |
3 | CONCAT(x, y); | 连接两个字符串x 和y ,并返回连接后的字符串。 |
4 | INITCAP(x); | 将x 中每个单词的初第一个字母转换为大写,并返回该字符串。 |
5 | INSTR(x, find_string [, start] [, occurrence]); | 在x 字符串中搜索find_string 子串并返回找到的位置。 |
6 | INSTRB(x); | 返回字符串x 在另一个字符串中第一次再现的位置,但返回值(以字节为单位)。 |
7 | LENGTH(x); | 返回x 中的字符数,也是计算字符串的长度。 |
8 | LENGTHB(x); | 返回单字节字符集的字符串长度(以字节为单位)。 |
9 | LOWER(x); | 将x 字符串中的字母转换为小写,并返回此小写字符串。 |
10 | LPAD(x, width [, pad_string]) ; | 使用空格垫放在x 字符串的左边,以使字符串的长度达到宽度字符。 |
11 | LTRIM(x [, trim_string]); | 修剪x 字符串左边的字符。 |
12 | NANVL(x, value); | 如果x 匹配NaN 特殊值(而不是数字),则返回值,否则返回x 字符串。 |
13 | NLS_INITCAP(x); | 与INITCAP(x) 函数相同,只不过它可以使用NLSSORT 指定的其他排序方法。 |
14 | NLS_LOWER(x) ; | 与LOWER(x) 函数相同,除了可以使用NLSSORT 指定的不同排序方法。 |
15 | NLS_UPPER(x); | 与UPPER() 函数相同,除了可以使用NLSSORT 指定的不同排序方法。 |
16 | NLSSORT(x); | 更改排序字符的方法。必须在任何NLS() 函数之前指定; 否则,将使用默认排序。 |
17 | NVL(x, value); | 如果x 为null 则返回value 值; 否则返回x 。 |
18 | NVL2(x, value1, value2); | 如果x 不为null 则返回值value1 ; 如果x 为null ,则返回value2 。 |
19 | REPLACE(x, search_string, replace_string); | 在x 字符串中搜索search_string 并将其替换为replace_string 。 |
20 | RPAD(x, width [, pad_string]); | 使用空格垫放在x 字符串的右边,以使字符串的长度达到宽度字符。 |
21 | RTRIM(x [, trim_string]); | 从右边修剪x 字符串。 |
22 | SOUNDEX(x) ; | 返回一个包含x 的语音表示的字符串。 |
23 | SUBSTR(x, start [, length]); | 返回x 字符串从指定start 位置开始到一个可选指定长度(length )范围内的子字符串。 |
24 | SUBSTRB(x); | 与SUBSTR() 相同,除了参数以字节表示,还支持单字节字符系统的字符。 |
25 | TRIM([trim_char FROM) x); | 修剪x 字符串的左边和右边的字符。 |
26 | UPPER(x); | 将x 中的字母转换为大写,并返回此大写后的字符串。 |
现在来看下面几个例子来了解这个概念 -
示例-1
SQL> DECLARE
2 greetings varchar2(11) := 'hello world';
3 BEGIN
4 dbms_output.put_line(UPPER(greetings));
5
6 dbms_output.put_line(LOWER(greetings));
7
8 dbms_output.put_line(INITCAP(greetings));
9
10 /* retrieve the first character in the string */
11 dbms_output.put_line ( SUBSTR (greetings, 1, 1));
12
13 /* retrieve the last character in the string */
14 dbms_output.put_line ( SUBSTR (greetings, -1, 1));
15
16 /* retrieve five characters,
17 starting from the seventh position. */
18 dbms_output.put_line ( SUBSTR (greetings, 7, 5));
19
20 /* retrieve the remainder of the string,
21 starting from the second position. */
22 dbms_output.put_line ( SUBSTR (greetings, 2));
23
24 /* find the location of the first "e" */
25 dbms_output.put_line ( INSTR (greetings, 'e'));
26 END;
27 /
HELLO WORLD
hello world
Hello World
h
d
world
ello world
2
PL/SQL procedure successfully completed
示例-2
SQL> declare
2 msg varchar2(30) := '......Hello World......';
3 begin
4 dbms_output.put_line(rtrim(msg,'.'));
5 dbms_output.put_line(ltrim(msg,'.'));
6 dbms_output.put_line(trim('.' from msg ));
7 end;
8 /
......Hello World
Hello World......
Hello World
PL/SQL procedure successfully completed