【问题标题】:How to remove replace part of a string?如何删除替换字符串的一部分?
【发布时间】:2018-07-23 07:23:54
【问题描述】:

我们有来自提供源帐户 ID 和数据库 ID 的用户的输入。这里的问题是输入将是:[222].[2]。我需要将第一个 [222] 过滤为源帐户 ID,将 [2] 中的第二个值过滤为 DB ID。

输入

[222].[2] ([srcacct id].[database id])

我们需要删除大括号"[""."

输出

Source Account id=222, DBID=2

PS:在 sql 中我有一个返回预期值的函数,但我不熟悉 Oracle,需要对此进行一些输入。

【问题讨论】:

  • 到目前为止您尝试过什么?提示:substr,instr
  • 我有一个函数可以在 sql 中执行此操作,但我不熟悉 Oracle。如果您需要我的 sql 查询,我可以提供相同的。
  • Instrsubstr 很有用;另一种方法可能是regexp_substr。尝试一些东西,如果你的代码有问题,请随时寻求帮助

标签: oracle oracle11g oracle10g


【解决方案1】:

在这种情况下,正则表达式非常简单; 源账户取第一个数字,DBID取第二个数字——它是REGEXP_SUBSTR函数的最后一个参数。

例如:

SQL> with test (col) as
  2    (select '[222].[2] ([srcacct id].[database id])' from dual)
  3  select regexp_substr(col, '\d+', 1, 1) source_account,
  4         regexp_substr(col, '\d+', 1, 2) dbid
  5  from test;

SOU D
--- -
222 2

SQL>

[编辑:变量]

这取决于您使用的客户端工具。在 SQL*Plus 中,您可以使用 '&&col'(或您想提供的任何名称)并运行

SQL> set ver off
SQL> select regexp_substr('&&col', '\d+', 1, 1) source_account,
  2         regexp_substr('&&col', '\d+', 1, 2) dbid
  3  from dual;
Enter value for col: [222].[2] ([srcacct id].[database id]

SOU D
--- -
222 2

SQL> undefine col

结束,取消定义它,因为“&&”将保持其值永远


无论如何,如果您想“传递”某些内容并“返回”结果,请考虑改为编写 函数。它无处不在。例如:

SQL> create or replace function f_ret (par_input in varchar2)
  2    return varchar2
  3  is
  4  begin
  5    return 'Source account ID = ' || regexp_substr(par_input, '\d+', 1, 1) ||
  6           ', DBID = '            || regexp_substr(par_input, '\d+', 1, 2);
  7  end;
  8  /

Function created.

SQL> select f_ret('[222].[2] ([srcacct id].[database id])') result from dual;

RESULT
--------------------------------------------------------------------------------
Source account ID = 222, DBID = 2

SQL>

[编辑#2:查找表]

您编写的代码(作为注释)无法编译;你不能那样做。这是它的外观示例。我现在无法测试它;自己做吧。

create or replace function f_ret (par_input in varchar2)
  return varchar2
is
  l_source_account_id number;
  l_dbid              number;
  retval              varchar2(200);
begin
  l_source_account_id := regexp_substr(par_input, '\d+', 1, 1);
  l_dbid              := regexp_substr(par_input, '\d+', 1, 2);

  select count(*)
    into l_cnt
    from accounts_table
    where srcaccountid = l_source_account_id;

  if l_cnt > 0 then
     retval := 'Source account ID = ' || regexp_substr(par_input, '\d+', 1, 1) ||
               ', DBID = '            || regexp_substr(par_input, '\d+', 1, 2);
  end if;

  return retval;            
end;

【讨论】:

  • 我在下面尝试过:DEF accountid varchar(40): = ('[123].[2].[2]'); select regexp_substr(@accountid, '\d+', 1, 1) source_account_id, regexp_substr(@accountid, '\d+', 1, 2) Database_id, regexp_substr(@accountid, '\d+', 1, 3) levelid from dual ;而且我缺少表达式错误。
  • 我需要知道如何将该值声明为变量并需要传递以获取结果
  • 我选择最后一个(函数)。我需要的是,我现在有一个表(源帐户 id、帐户 id、db id 列),我需要过滤源帐户 id 并根据输入的源帐户值仅返回帐户 id。
  • 请随意。
  • 我试过这个:SQL> 创建或替换函数 get_accountdetails (par_input in varchar2) return varchar2 is begin 'Source_Account_ID = ' || regexp_substr(par_input, '\d+', 1, 1) || ', DBID = ' || regexp_substr(par_input, '\d+', 1, 2) || ',Account_Level = ' || regexp_substr(par_input, '\d+', 1, 3); return 'Account_id =' select account_id from table a where srcaccountid=Source_Account_ID end; /
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-18
  • 1970-01-01
  • 2012-03-31
相关资源
最近更新 更多