【问题标题】:Convert SQL Server function into Oracle将 SQL Server 函数转换为 Oracle
【发布时间】:2021-06-14 02:51:03
【问题描述】:

这是我的 SQL Server 函数,我正在尝试将大写转换为正确的大小写,它在 SQL Server 中成功运行,但在 Oracle 中却没有,有人可以帮我转换以下代码:

CREATE FUNCTION CamelCase(@Text as VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
 DECLARE @Index INT;
 DECLARE @CurChar CHAR(1);
 DECLARE @Reset BIT;
 DECLARE @Ret VARCHAR(8000);

 IF @Text IS NULL
   RETURN NULL;

 SELECT @Reset = 1, 
        @Index = 1,
        @Ret = '';

 WHILE (@Index <= len(@Text))

 SELECT @CurChar = substring(@Text, @Index, 1),
        @Ret = @Ret + CASE WHEN @Reset = 1 THEN UPPER(@CurChar) ELSE LOWER(@CurChar) END,
        @Reset = CASE WHEN @CurChar like '[a-zA-Z]' THEN 0 ELSE 1 END,
        @Index = @Index + 1
RETURN @Ret
END

我尝试将其翻译成如下所示的Oracle函数代码:

create or replace function  CamelCase(Text in VARCHAR2)
RETURN VARCHAR2
IS
 Ret VARCHAR2(8000):= '';
 Index NUMBER:= 1;
 CurChar CHAR0;
 Resets BIT := 1;
BEGIN    
 WHILE (Index <= LENGTH(Text))
 loop
    CurChar := SUBSTR(Text, Index, 1);
    if (Reset = 1) then 
          ret:= UPPER(CurChar) ;
        ELSE 
         ret:= LOWER(CurChar);
    end if ;
    if (CurChar like '[a-zA-Z]') then 
    reset:=  0;
    else 
      reset:=  1;
    end if ;
    Index := Index + 1;
 end loop;      
 RETURN Ret;
END

但这似乎是错误的,请有人帮我转换一下。

【问题讨论】:

  • 老实说,我会尝试在两种环境中修复整个问题。任一 RDBMS 上的循环都会非常缓慢。
  • 它的目的是什么?
  • 谢谢大家,将大写转换为正确的大小写。
  • 与其循环使用 UPPER 和 LOWER,你考虑过只使用 INITCAP 吗?
  • 不,我没有因为可能的话我需要完全转换这个函数。谢谢

标签: sql sql-server oracle function plsql


【解决方案1】:

这样可以吗?查看代码中的 cmets。

SQL> create or replace function f_proper (par_text in varchar2)
  2    return varchar2
  3  is
  4    l_this varchar2(1);   -- current character
  5    l_prev varchar2(1);   -- previous character
  6    l_res varchar2(200);  -- resulting string
  7  begin
  8    if par_text is null then
  9       return null;
 10    end if;
 11
 12    for i in 1 .. length(par_text) loop
 13      l_this := substr(par_text, i, 1);
 14      l_prev := substr(par_text, i - 1, 1);
 15
 16      l_res := l_res ||
 17        -- If I'm on the first character of the string OR
 18        -- previous character is a space and current character is not a space
 19        -- then return UPPERCASE of the current character.
 20        -- Else, return lowercase of the current character.
 21        case when i = 1 or
 22                  (l_prev  = ' ' and l_this <> ' ')
 23             then upper(l_this)
 24             else lower(l_this)
 25        end;
 26    end loop;
 27
 28    return l_res;
 29  end;
 30  /

Function created.

测试:

SQL> select f_proper('little  fo ot') r1,
  2         f_proper('I tried to translate it into Oracle') r2,
  3         f_proper('ABC') r3,
  4         f_proper(null) r4
  5  from dual;

R1              R2                                  R3  R4
--------------- ----------------------------------- --- ---
Little  Fo Ot   I Tried To Translate It Into Oracle Abc

SQL>

或者,不用重新发明轮子,使用内置的initcap 函数:

SQL> select initcap('little  fo ot') r1,
  2         initcap('I tried to translate it into Oracle') r2,
  3         initcap('ABC') r3,
  4         initcap(null) r4
  5  from dual;

R1              R2                                  R3  R4
--------------- ----------------------------------- --- ---
Little  Fo Ot   I Tried To Translate It Into Oracle Abc

SQL>

【讨论】:

  • 谢谢,这正是我所期待的,再次感谢。
猜你喜欢
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2018-04-15
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多