📅  最后修改于: 2020-11-26 05:53:51             🧑  作者: Mango
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提供的字符串函数-
S.No | Function & Purpose |
---|---|
1 |
ASCII(x); Returns the ASCII value of the character x. |
2 |
CHR(x); Returns the character with the ASCII value of x. |
3 |
CONCAT(x, y); Concatenates the strings x and y and returns the appended string. |
4 |
INITCAP(x); Converts the initial letter of each word in x to uppercase and returns that string. |
5 |
INSTR(x, find_string [, start] [, occurrence]); Searches for find_string in x and returns the position at which it occurs. |
6 |
INSTRB(x); Returns the location of a string within another string, but returns the value in bytes. |
7 |
LENGTH(x); Returns the number of characters in x. |
8 |
LENGTHB(x); Returns the length of a character string in bytes for single byte character set. |
9 |
LOWER(x); Converts the letters in x to lowercase and returns that string. |
10 |
LPAD(x, width [, pad_string]) ; Pads x with spaces to the left, to bring the total length of the string up to width characters. |
11 |
LTRIM(x [, trim_string]); Trims characters from the left of x. |
12 |
NANVL(x, value); Returns value if x matches the NaN special value (not a number), otherwise x is returned. |
13 |
NLS_INITCAP(x); Same as the INITCAP function except that it can use a different sort method as specified by NLSSORT. |
14 |
NLS_LOWER(x) ; Same as the LOWER function except that it can use a different sort method as specified by NLSSORT. |
15 |
NLS_UPPER(x); Same as the UPPER function except that it can use a different sort method as specified by NLSSORT. |
16 |
NLSSORT(x); Changes the method of sorting the characters. Must be specified before any NLS function; otherwise, the default sort will be used. |
17 |
NVL(x, value); Returns value if x is null; otherwise, x is returned. |
18 |
NVL2(x, value1, value2); Returns value1 if x is not null; if x is null, value2 is returned. |
19 |
REPLACE(x, search_string, replace_string); Searches x for search_string and replaces it with replace_string. |
20 |
RPAD(x, width [, pad_string]); Pads x to the right. |
21 |
RTRIM(x [, trim_string]); Trims x from the right. |
22 |
SOUNDEX(x) ; Returns a string containing the phonetic representation of x. |
23 |
SUBSTR(x, start [, length]); Returns a substring of x that begins at the position specified by start. An optional length for the substring may be supplied. |
24 |
SUBSTRB(x); Same as SUBSTR except that the parameters are expressed in bytes instead of characters for the single-byte character systems. |
25 |
TRIM([trim_char FROM) x); Trims characters from the left and right of x. |
26 |
UPPER(x); Converts the letters in x to uppercase and returns that string. |
现在让我们通过一些例子来理解这个概念-
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.
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.