简体   繁体   English

从 PL/SQL 中的 T-SQL 复制 STUFF() 函数的最佳方法是什么?

[英]What is the best way to replicate the STUFF() function from T-SQL in PL/SQL?

I'm trying to replicate the behavior of the STUFF() function from Transact SQL in Oracle.我正在尝试从 Oracle 中的 Transact SQL 复制STUFF()函数的行为。 I assume both the CONCAT and SUBSTR functions will be involved, but I can't figure out a clever way to do it without writing too much code.我假设将涉及CONCATSUBSTR函数,但我想不出一个聪明的方法来完成它而不编写太多代码。

Does anyone know a fancy way to achieve this?有谁知道实现这一目标的奇特方法?

As there's no such built-in function, as you said - you'll have to write one for your own, using concatenation and substr function.由于没有这样的内置函数,正如您所说 - 您必须使用连接和substr函数为自己编写一个。 Something like this:像这样的东西:

SQL> create or replace function stuff
  2    (par_str in varchar2,
  3     par_start in number,
  4     par_length in number,
  5     par_replace_with in varchar2
  6    )
  7    return varchar2
  8  is
  9  begin
 10    return substr(par_str, 1, par_start - 1) ||
 11           par_replace_with ||
 12           substr(par_str, par_start + par_length);
 13  end stuff;
 14  /

Function created.

SQL> select stuff('abcdef', 2, 3, 'ijklmn') result from dual;

RESULT
--------------------------------------------------------------------------------
aijklmnef

SQL>

(Simulated using STUFF T-SQL documentation) (使用STUFF T-SQL文档模拟)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM