PL/SQL 专题
您的位置:database > PL/SQL专题 > PL/SQL字符串
PL/SQL字符串
作者:--    发布时间:2019-11-20

pl/sql中的字符串实际上是一个具有可选大小规格的字符序列。字符可以是数字,字母,空白,特殊字符或全部的组合。 pl/sql提供三种字符串 -

  • 固定长度字符串 - 在这样的字符串中,程序员在声明字符串时指定长度。该字符串的右边填充规定的长度。
  • 可变长度字符串 - 在这样的字符串中,指定字符串的最大长度达32,767,并且不会填充。
  • 字符大对象(clob) - 这些可变长度字符串最多可达128tb

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

'this is a string literal.' 
--或者 
'hello world'

要在字符串文字中包含单引号,需要在彼此之间键入两个单引号。 例如,

'this isn''t what it looks like'

声明字符串变量

oracle数据库提供了许多字符串数据类型,如:charncharvarchar2nvarchar2clobnclob。 以“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提供用于连接两个字符串的级联运算符(||)。 下表提供了pl/sql提供的字符串函数 -

编号 函数 描述
1 ascii(x); 返回字符x的ascii值。
2 chr(x); 返回ascii值为x的字符。
3 concat(x, y); 连接两个字符串xy,并返回连接后的字符串。
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); 如果xnull则返回value值; 否则返回x
18 nvl2(x, value1, value2); 如果x不为null则返回值value1; 如果xnull,则返回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提示符下执行时,它会产生以下结果 -


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册