PL/SQL - 字串



PL/SQL 中的字串實際上是一系列字元,並帶有一個可選的大小規範。這些字元可以是數字、字母、空格、特殊字元或所有字元的組合。PL/SQL 提供三種字串:

  • 定長字串 - 在此類字串中,程式設計師在宣告字串時指定長度。字串在右側用空格填充到指定的長度。

  • 變長字串 - 在此類字串中,指定了字串的最大長度(最多 32,767),並且不進行填充。

  • 字元大物件 (CLOB) - 這些是變長字串,最大可達 128 TB。

PL/SQL 字串可以是變數或字面量。字串字面量用引號括起來。例如:

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

要在字串字面量中包含單引號,您需要鍵入兩個單引號。例如:

'this isn''t what it looks like'

宣告字串變數

Oracle 資料庫提供了許多字串資料型別,例如 CHAR、NCHAR、VARCHAR2、NVARCHAR2、CLOB 和 NCLOB。以'N'為字首的資料型別是'國家字元集'資料型別,用於儲存 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

要宣告定長字串,請使用 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);

返回一個字串在另一個字串中的位置,但以位元組為單位返回該值。

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的子字串,該子字串從 start 指定的位置開始。可以提供子字串的可選長度。

24

SUBSTRB(x);

與 SUBSTR 相同,只是引數以位元組而不是字元表示(對於單位元組字元系統)。

25

TRIM([trim_char FROM) x);

x的左側和右側修剪字元。

26

UPPER(x);

將 x 中的字母轉換為大寫並返回該字串。

現在讓我們透過一些示例來理解這個概念:

示例 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.

示例 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. 
廣告