pl/sql中的字符串实际上是一个具有可选大小规格的字符序列。字符可以是数字,字母,空白,特殊字符或全部的组合。 pl/sql提供三种字符串 -
32,767
,并且不会填充。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
数据类型。 以下示例说明声明和使用一些字符串变量 -
set serveroutput on size 99999;
declare
name varchar2(20);
company varchar2(30);
introduction clob;
choice char(1);
begin
name := 'max su';
company := 'hixiaoniu';
introduction := ' hello! i''m max su from hixiaoniu.';
choice := 'y';
if choice = 'y' then
dbms_output.put_line(name);
dbms_output.put_line(company);
dbms_output.put_line(introduction);
end if;
end;
/
当上述代码在sqlplus提示符下执行时,它会产生以下结果 -
要声明一个固定长度的字符串,请使用char
数据类型。 在这里,不必为固定长度变量指定最大长度。 如果不考虑长度约束,oracle数据库将自动使用所需的最大长度。以下两个声明是相同的 -
red_flag char(1) := 'y';
red_flag char := 'y';
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
set serveroutput on size 99999;
declare
greetings varchar2(11) := 'hello world';
begin
dbms_output.put_line(upper(greetings));
dbms_output.put_line(lower(greetings));
dbms_output.put_line(initcap(greetings));
/* retrieve the first character in the string */
dbms_output.put_line ( substr (greetings, 1, 1));
/* retrieve the last character in the string */
dbms_output.put_line ( substr (greetings, -1, 1));
/* retrieve five characters,
starting from the seventh position. */
dbms_output.put_line ( substr (greetings, 7, 5));
/* retrieve the remainder of the string,
starting from the second position. */
dbms_output.put_line ( substr (greetings, 2));
/* find the location of the first "e" */
dbms_output.put_line ( instr (greetings, 'e'));
end;
/
当上述代码在sqlplus提示符下执行时,它会产生以下结果 -
hello world
hello world
hello world
h
d
world
ello world
2
pl/sql 过程已成功完成。
示例-2
set serveroutput on size 99999;
declare
greetings varchar2(30) := '......hello world.....';
begin
dbms_output.put_line(rtrim(greetings,'.'));
dbms_output.put_line(ltrim(greetings, '.'));
dbms_output.put_line(trim( '.' from greetings));
end;
/
当上述代码在sqlplus提示符下执行时,它会产生以下结果 -