Plsql 简明教程

PL/SQL - Strings

PL/SQL 中的字符串实际上是一系列字符,可选择指定大小。这些字符可以是数字、字母、空格、特殊字符或它们的组合。PL/SQL 提供了三种字符串——

  1. Fixed-length strings ——在这种字符串中,程序员在声明字符串时指定长度。该字符串将右填充空格到指定的长度。

  2. Variable-length strings ——在这种字符串中,指定字符串的最大长度(最长为 32,767),不会进行填充。

  3. Character large objects (CLOBs) ——这些是可变长度字符串,最大可达 128 TB。

PL/SQL 字符串可以是变量或文本。字符串文本用引号括起来。例如,

'This is a string literal.' Or 'hello world'

要在一串文字中包含一个单引号,您需要将两个单引号放在一起。例如,

'this isn''t what it looks like'

Declaring String Variables

Oracle 数据库提供了多种字符串数据类型,例如 CHAR、NCHAR、VARCHAR2、NVARCHAR2、CLOB 和 NCLOB。前缀为 'N' 的数据类型是 'national character set' 数据类型,用于存储 Unicode 字符数据。

如果您需要声明一个可变长度字符串,您必须提供该字符串的最大长度。例如,VARCHAR2 数据类型。以下示例说明如何声明并使用一些字符串变量——

DECLARE
   name varchar2(20);
   company varchar2(30);
   introduction clob;
   choice char(1);
BEGIN
   name := 'John Smith';
   company := 'Infotech';
   introduction := ' Hello! I''m John Smith from Infotech.';
   choice := 'y';
   IF choice = 'y' THEN
      dbms_output.put_line(name);
      dbms_output.put_line(company);
      dbms_output.put_line(introduction);
   END IF;
END;
/

当以上代码在 SQL 提示符下执行时,它会生成以下结果:

John Smith
Infotech
Hello! I'm John Smith from Infotech.

PL/SQL procedure successfully completed

To declare a fixed-length string, use the CHAR datatype. Here you do not have to specify a maximum length for a fixed-length variable. If you leave off the length constraint, Oracle Database automatically uses a maximum length required. The following two declarations are identical −

red_flag CHAR(1) := 'Y';
 red_flag CHAR   := 'Y';

PL/SQL String Functions and Operators

PL/SQL offers the concatenation operator (||) for joining two strings. The following table provides the string functions provided by PL/SQL −

S.No

Function & Purpose

1

ASCII(x); Returns the ASCII value of the character x.

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); 返回字符串中另一个字符串的位置,但以字节方式返回该值。

7

LENGTH(x); 返回 x 中的字符数。

8

LENGTHB(x); 以字节为单位返回单字节字符集中字符字符串的长度。

9

LOWER(x); 将 x 中的字母转换为小写并返回该字符串。

10

LPAD(x, width [, pad_string]) ; 使用空格填充 x ,以使字符串的总长度达到 width 个字符。

11

LTRIM(x [, trim_string]);x 的左侧修剪字符。

12

NANVL(x, value); 如果 x 匹配 NaN 特殊值(非数字),则返回 value,否则返回 x

13

NLS_INITCAP(x); 与 INITCAP 函数相同,除了它可以使用 NLSSORT 指定的不同排序方法。

14

NLS_LOWER(x) ; 与 LOWER 函数相同,除了它可以使用 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 的子串。可以提供子串的可选长度。

24

SUBSTRB(x); 与 SUBSTR 相同,不同之处在于对于单字节字符系统,参数以字节而不是字符表示。

25

TRIM([trim_char FROM) x);x 的左边和右边修剪字符。

26

UPPER(x); 将 x 中的字母转换为大写并返回该字符串。

现在让我们通过几个示例来理解这个概念 -

Example 1

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;
/

当以上代码在 SQL 提示符下执行时,它会生成以下结果:

HELLO WORLD
hello world
Hello World
h
d
World
ello World
2

PL/SQL procedure successfully completed.

Example 2

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;
/

当以上代码在 SQL 提示符下执行时,它会生成以下结果:

......Hello World
Hello World.....
Hello World

PL/SQL procedure successfully completed.