我正在尝试使用varchar2作为过程的输入并将其格式化,然后使用新字符串进行一些计算。输入将是一系列用逗号分隔的名称:
示例:名称,ID,地址
我需要找到一种方法来将以上内容更改为:
CAST(name AS VARCHAR2(200)) name,
CAST(id AS VARCHAR2(200)) id,
CAST(address AS VARCHAR2(200)) address
我尝试查看,REGEXP_REPLACE
但我是初学者,所以我不知道如何正确使用正则表达式。
(顺便说一下,这是针对Oracle SQL的)
您实际上并不需要正则表达式;instr
+substr
做得好,因为这是非常简单的任务。
SQL> create or replace procedure p_test (par_string in varchar2) is
2 -- comma positions
3 l_pos1 number := instr(par_string, ',', 1, 1);
4 l_pos2 number := instr(par_string, ',', 1, 2);
5 l_pos3 number := instr(par_string, ',', 1, 3);
6 -- variables
7 l_name varchar2(200);
8 l_id varchar2(200);
9 l_address varchar2(200);
10 begin
11 l_name := substr(par_string, 1, l_pos1 - 1);
12 l_id := substr(par_string, l_pos1 + 1, l_pos2 - l_pos1 - 1);
13 l_address := substr(par_string, l_pos2 + 1);
14
15 dbms_output.put_line('Name = ' || l_name);
16 dbms_output.put_line('ID = ' || l_id);
17 dbms_output.put_line('Address = ' || l_address);
18 end;
19 /
Procedure created.
测试:
SQL> set serveroutput on
SQL> exec p_test('Little Foot,25,Manhattan 23 New York');
Name = Little Foot
ID = 25
Address = Manhattan 23 New York
PL/SQL procedure successfully completed.
SQL>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句