PL/SQL字串實際上是一個可選的尺寸規格字符序列。字符可以是數字,字母,空白,特殊字元或全部的組合。 PL/SQL提供了三種類型的字串:
- 固定長度字串:在這樣的字串,程序員指定的長度,同時聲明該字串。該字串是右填充空格以達到指定的長度。
 - 變動字串:在這樣的字串,最大長度可達32,767,為字串指定,並不需要填充。
 - 大物件字元(CLOB):這是可變長度的字串,可以達到128兆兆字節。
 
PL/ SQL字串可以是變數或字面值。字串文字被引號圍在內部。例如,
'This is a string literal. gitbook.net' 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; 
/ 
 | 
結果如下:
John Smith 
Infotech Corporation 
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提供的字串功能(函數):
S.N. 
 | 
函數及用途 
 | 
1 
 | 
ASCII(x); 
回傳字元 x 的 ASCII 值 
 | 
2 
 | 
CHR(x); 
返回字符 x 的 ASCII 值 
 | 
3 
 | 
CONCAT(x, y); 
連接字串x和y,並返回附加的字串 
 | 
4 
 | 
INITCAP(x); 
每個單詞的首字母x中轉換為大寫,並返回該字串 
 | 
5 
 | 
INSTR(x, find_string [, start] [, occurrence]); 
搜索find_string在x中並返回它出現的位置 
 | 
6 
 | 
INSTRB(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函數,但它可以使用不同的排序方法所指定NLSSORT 
 | 
14 
 | 
NLS_LOWER(x) ; 
同樣的,不同的是它可以使用不同的排序方法所指定NLSSORT LOWER函數 
 | 
15 
 | 
NLS_UPPER(x); 
相同,不同之處在於它可以使用不同的排序方法所指定NLSSORT UPPER函數 
 | 
16 
 | 
NLSSORT(x); 
改變排序的字符的方法。任何NLS函數之前必須指定該參數;否則,默認的排序被使用 
 | 
17 
 | 
NVL(x, value); 
回傳如果x為null返回null; 否則返回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; 
/ 
 | 
產生了以下結果:
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; 
/ 
 | 
產生了以下結果:
......Hello World 
Hello World..... 
Hello World 
PL/SQL procedure successfully completed. 
 | 
沒有留言:
張貼留言