PL/SQL字符串声明、字符串函数和操作符实例源码说明

            PL/SQL字符串实际上是一个可选的尺寸规格字符序列。字符可以是数字,字母,空白,特殊字符或全部的组合。 PL/SQL提供了三种类型的字符串:

  • 固定长度字符串在这样的字符串,程序员指定的长度,同时声明该字符串。该字符串是右填充空格以达到指定的长度。

  • 变长字符串在这样的字符串,最大长度可达32,767,为字符串指定,并不需要填充。

  • 字符大对象(CLOB)这是可变长度的字符串,可以达到128兆兆字节。

声明字符串变量

        Oracle数据库提供了大量的字符串数据类型,如:CHAR,NCHAR,VARCHAR2,NVARCHAR2,CLOB和NCLOB。前面加上一个'N'的数据类型为“国家字符集”数据类型,即存储Unicode字符数据。如果需要声明一个可变长度的字符串时,必须提供该字符串的最大长度。

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转换为大写字母,并返回该字符串

实例源码说明:

-- Created on 2018/3/26 by E.WANG 
/*
字符可以使数字、字母、特殊字符或全部的组合。
Oracle数据库提供了大量的字符串数据类型,
如:CHAR,NCHAR,VARCHAR2,NVARCHAR2,CLOB和NCLOB。
前面加上一个'N'的数据类型为“国家字符集”数据类型,即存储Unicode字符数据。
如果需要声明一个可变长度的字符串时,必须提供该字符串的最大长度。
*/

declare 
  --固定长度的字符串。
  --字符串右边填充空格以达到指定的长度。
  char_string char(3);
  --变长字符串。最大长度可达32767,为字符串指定,并不需要填充。
  varchar_string varchar2(50);
  
  --字符大对象。可变长度的字符串,可以达到128兆字节。
  clob_string clob;
 
begin
  -- Test statements here
  char_string:='yes';
  varchar_string:='I like oracle.becasue it is so powerful!';
  clob_string:='Oracle Corporation is an American multinational computer technology corporation, headquartered in Redwood Shores, California.The company specializes primarily in developing and marketing database software and technology, cloud engineered systems and enterprise software products — particularly its own brands of database management systems. ]In 2015, Oracle was the second-largest software maker by revenue, after Microsoft.
The company also develops and builds tools for database development and systems of middle-tier software, enterprise resource planning (ERP) software, customer relationship management (CRM) software and supply chain management (SCM) software.';
  
  if char_string='yes' then
     dbms_output.put_line(varchar_string);
     dbms_output.put_line(clob_string);
  end if;
  
  --调用ascii(x)函数返回字符x的accii值
  dbms_output.put_line('the a ascii value is: ' || ascii('a'));
  
  --调用chr(x)返回ascii对应的字符
  dbms_output.put_line('the 97 ascii value is: ' || chr(97));
  
  --调用concat(x,y)连接字符串x和y,并返回附加的字符串。
  dbms_output.put_line('hello, +world= ' || concat('hello,','world')); 
 
  --调用initcap(x)每个单词的首字母转换为大写,并返回该字符串
  dbms_output.put_line('initcap(hello)----' || initcap('hello'));  
  
  --调用instr(x, find_string [, start] [, occurrence])
  --搜索find_string在x中并返回它出现的位置。
  dbms_output.put_line(varchar_string||'find like position :' || instr(varchar_string,'like')); 
  
  --调用instrb(x)
  --搜索find_string在x中并返回它出现的位置。
  dbms_output.put_line(varchar_string||'find like position :' || instrb(varchar_string,'like')); 
  
  --调用length(x):返回x中的字符数
  dbms_output.put_line('the length of ''hello,world!'' is:' || length('hello,world!')); 
  
  --调用lengthb(x):返回为单字节字符集的字节的字符串的长度
  dbms_output.put_line('the lengthb of ''hello,world!'' is:' || lengthb('hello,world!')); 
  
  --调用lower(x):把x转换为小写字母并返回
  dbms_output.put_line('''HELLO,WORLD!''---' || lower('HELLO,WORLD!'));
  
  --调用lpad(x,width[,pad_string]):x用空格向左填充,把字符串的总长度达到width
  dbms_output.put_line('''HELLO,WORLD!''---:' || lpad('HELLO,WORLD!',35));
  
  --调用ltrim(x[,trim_string]):从x的左边修剪字符
  dbms_output.put_line('  HELLO,WORLD!'|| ltrim('  HELLO,WORLD!'));
  
  --调用nanvl(x,value):如果x是数字就返回x,否则返回value
  dbms_output.put_line('HELLO,WORLD!-----------'||nanvl('123','456'));
   
  --调用nlssort(x):改变排序的字符的方法。任何NLS函数之前必须指定该参数;否则,默认的排序被使用
  dbms_output.put_line('HeLLo_nlssort(x)!----'||nlssort('HeLLo'));
  
  --调用nls_initcap(x):相同initcap函数,但它可以使用不同的排序方法所指定NLSSORT
  
  dbms_output.put_line('hello_nls_initcap!------'||nls_initcap('hello'));

   --调用nls_lower(x):同样lower函数,不同的是它可以使用不同的排序方法所指定NLSSORT LOWER函数
  
  dbms_output.put_line('HeLLo_nls_lower!----'||nls_lower('HeLLo'));
  
   --调用nls_upper(x):同样upper函数,不同的是它可以使用不同的排序方法所指定NLSSORT UPPER函数
  dbms_output.put_line('HeLLo_nls_upper!----'||nls_upper('HeLLo'));
  
  --调用nvl(x,value):返回如果x为null返回value; 否则返回x
  dbms_output.put_line('null_nvl!----'||nvl('','HeLLo'));
  dbms_output.put_line('null_nvl!----'||nvl('NULL','HeLLo'));
  
  --调用nvl2(x,value1,value2):返回如果x为null返回value2; 否则返回value1
  --dbms_output.put_line('null_nvl2!----'||nvl2('','value1','value2'));
  --dbms_output.put_line('null_nvl2!----'||nvl2('NULL','value1','value2'));
  
  --调用replace(x, search_string, replace_string):从x中查找search_string字符串并用replace_string替换
  dbms_output.put_line(varchar_string||'----'||replace(varchar_string,'like','love'));
  
   --调用rpad(x, width[, pad_string]):填充到x的右侧,如果width长度比x长,如果没有指定pad_string,则填空。
  dbms_output.put_line('hello----'||rpad('hello',11,',world'));
  dbms_output.put_line('hello----'||rpad('hello',11));
  
  --调用rtrim(x, [, trim_string]):从x右边修剪。
  dbms_output.put_line('hello----'||rtrim('hello  '));
  dbms_output.put_line('hello----'||rtrim('hello----','----')); 
  
  --调用soundex(x):返回包含x的拼音表示形式的字符串
  dbms_output.put_line('hello----'||soundex('hello'));
  
  --调用substr(x,start[,length]):返回x的一个子开始于由start指定的位置。可选长度为子字符串。
  dbms_output.put_line('hello----'||substr('hello',3,2));
  dbms_output.put_line('hello----'||substr('hello',3));
  
  --调用substrb(x,start[,length]):相同substr除外的参数均以字节代替字符的单字节字符的系统
  dbms_output.put_line('I like oracle----'||substr('I like oracle',3,4));
  dbms_output.put_line('I like oracle----'||substr('I like oracle',3));
  
  --调用trim(x, [, trim_string):从左侧和右侧修剪空白字符
  dbms_output.put_line('I like oracle----'||trim(' like '));
  
  --调用upper(x):x转换为大写字母,并返回该字符串
  dbms_output.put_line('I like oracle----'||upper('I like oracle'));
end;

窗口截图:


运行结果截图:



猜你喜欢

转载自blog.csdn.net/henni_719/article/details/79727332