Home > other >  while using MOD in oracle sql, i am getting error
while using MOD in oracle sql, i am getting error

Time:11-30

This is my code- code

error- number or types of arguments are calling =

i think it's a syntax error. i am getting same error in most of my codes

CodePudding user response:

Wrong syntax.

SQL> CREATE OR REPLACE FUNCTION comparisonlike (g IN VARCHAR2)
  2     RETURN VARCHAR2
  3  AS
  4  BEGIN
  5     RETURN CASE WHEN MOD (g, 2) = 0 THEN 'EVEN' ELSE 'ODD' END;
  6  END;
  7  /

Function created.

SQL> select comparisonlike(25) result from dual;

RESULT
--------------------
ODD

SQL> select comparisonlike(12) result from dual;

RESULT
--------------------
EVEN

SQL>

If you want to check digit anchored to the end of the string, then

SQL> CREATE OR REPLACE FUNCTION comparisonlike (g IN VARCHAR2)
  2     RETURN VARCHAR2
  3  AS
  4  BEGIN
  5     RETURN CASE WHEN MOD (TO_NUMBER(REGEXP_SUBSTR(g, '\d$')), 2) = 0 THEN 'EVEN'
  6                 ELSE 'ODD'
  7            END;
  8  END;
  9  /

Function created.

SQL> SELECT comparisonlike(22) res_1,                    --> 22 is being checked
  2         comparisonlike('1st great result13') res_2   --> 13 is being checked
  3  FROM dual;

RES_1      RES_2
---------- ----------
EVEN       ODD

SQL>
  • Related