建立表:
1 create table USER_INFO
2 (
3 user_name VARCHAR2(100),
4 id VARCHAR2(32)
5 )
插入数据
1 insert into USER_INFO (USER_NAME, ID)
2 values ('小明', '1');
3
4 insert into USER_INFO (USER_NAME, ID)
5 values ('小刚', '3');
6
7 insert into USER_INFO (USER_NAME, ID)
8 values ('小陈', '4');
9
10 insert into USER_INFO (USER_NAME, ID)
11 values ('小红', '2');
建立函数,根据ID获取名字:
1 CREATE OR REPLACE FUNCTION GETUSERNAMEBYID(I_ID VARCHAR2) RETURN VARCHAR2 IS
2 V_SQL VARCHAR2(100);
3 V_USER_NAME VARCHAR2(100);
4 BEGIN
5 IF I_ID IS NULL THEN
6 RETURN '';
7 END IF;
8 SELECT USER_NAME INTO V_USER_NAME FROM USER_INFO U WHERE U.ID = I_ID;
9 RETURN V_USER_NAME;
10 EXCEPTION
11 WHEN OTHERS THEN
12 RETURN '';
13 END;
传入ID串,返回名字串
1 CREATE OR REPLACE FUNCTION GET_USER_NAMES(I_IDS IN CLOB) RETURN CLOB IS
2 V_IDS CLOB;
3 V_ID VARCHAR2(32);
4 V_USERNAMES CLOB;
5 V_USERNAME VARCHAR2(100);
6 BEGIN
7 --判断是否为空
8 IF I_IDS IS NULL THEN
9 RETURN '';
10 END IF;
11 BEGIN
12 --截掉空格
13 V_IDS := TRIM(I_IDS);
14 --判断末尾是否有逗号分隔符:添加逗号分隔符
15 IF SUBSTR(I_IDS, LENGTH(I_IDS)) != ';' THEN
16 V_IDS := V_IDS || ';';
17 END IF;
18 WHILE INSTR(V_IDS, ';') != 0 LOOP
19 --截取第一个ID
20 V_ID := SUBSTR(V_IDS, 1, INSTR(V_IDS, ';') - 1);
21
22 --重新赋值
23 V_IDS := SUBSTR(V_IDS, INSTR(V_IDS, ';') + 1);
24
25 DBMS_OUTPUT.PUT_LINE(V_IDS);
26 --根据ID取得用户的名字
27 V_USERNAME := GETUSERNAMEBYID(I_ID => V_ID);
28
29 IF V_USERNAME = '' THEN
30 V_USERNAME := V_ID;
31 END IF;
32 IF V_USERNAMES IS NULL THEN
33 V_USERNAMES := V_USERNAME;
34 ELSE
35 V_USERNAMES := V_USERNAMES || ';' || V_USERNAME;
36 END IF;
37 END LOOP;
38 DBMS_OUTPUT.PUT_LINE(V_USERNAMES);
39 IF SUBSTR(V_USERNAMES, LENGTH(V_USERNAMES)) = ';' THEN
40 V_USERNAMES := SUBSTR(V_USERNAMES, 1, LENGTH(V_USERNAMES) - 1);
41 END IF;
42 EXCEPTION
43 WHEN OTHERS THEN
44 --V_USERNAMES := I_IDS;
45 NULL;
46 END;
47 --返回
48 RETURN V_USERNAMES;
49 END;
测试:
Trim函数的介绍:
TRIM
enables you to trim leading or trailing characters (or both) from a character string. If trim_character
or trim_source
is a character literal, then you must enclose it in single quotes.
- If you specify
LEADING
, then Oracle removes any leading characters equal totrim_character
. - If you specify
TRAILING
, then Oracle removes any trailing characters equal totrim_character
. - If you specify
BOTH
or none of the three, then Oracle removes leading and trailing characters equal totrim_character
. - If you do not specify
trim_character
, then the default value is a blank space. - If you specify only
trim_source
, then Oracle removes leading and trailing blank spaces. - The function returns a value with datatype
VARCHAR2
. The maximum length of the value is the length oftrim_source
. - If either
trim_source
ortrim_character
is null, then theTRIM
function returns null.
Both trim_character
and trim_source
can be any of the datatypes CHAR
, VARCHAR2
, NCHAR
, NVARCHAR2
, CLOB
, or NCLOB
. The string returned is of VARCHAR2
datatype and is in the same character set as trim_source
.
Trim语法:
Oracle TRIM函数的完整语法描述
TRIM([ { { LEADING | TRAILING | BOTH }
[ trim_character ]
| trim_character
}
FROM
]
trim_source
)
LEADING | TRAILING | BOTH 三个参数的意义:
首部、尾部、首部和尾部
Examples:
1、默认截取首部和尾部的空格:
1 SELECT TRIM(' Hello World ') FROM dual
结果:Hello World
也可以使用:
1 SELECT TRIM(BOTH FROM ' Hello World ') FROM dual
2、Trailing参数的使用;截掉尾部的。
1 select trim(trailing from ' hello world ') from dual
结果: hello world
3、LEADING 参数的使用;截掉首部的。
1 select trim(LEADING from ' hello world ') from dual
结果:hello world
4、自定义参数的使用:截掉首部和尾部的o
1 SELECT TRIM(both 'o' FROM 'ohello worldo') FROM DUAL
结果:hello world
但是他只支持单字符的,不支持多字符:如下:
1 SELECT TRIM(both 'ho' FROM 'ohello worldo') FROM DUAL
可以使用LTRIM和RTRIM实现相同的功能:
1、LTRIM:实现删除左侧还有oa两个字符中的任意的字符,截止第一个不是它们之中的字符
1 SELECT LTRIM('OaOOOaaaHELLO WORLDO','Oa') FROM DUAL
结果:HELLO WORLDO
RTRIM类似
同时使用解决上面的限制单个字符的问题:
1 select ltrim(rtrim('abHELLO WORLDab','ab'),'ba') from dual;
a和b的顺序没什么要求
结果:HELLO WORLD
使用RTRIM和LTRIM函数时的注意事项:“ab”不表示整个“ab”字符串进行匹配,而是发现任意的字符“a”或字符“b”均做删除操作。