Oracle SQL function overloading

19 篇文章 0 订阅

Sample of implement Oracle SQL function overloading, and default parameter.


To define your own to_char(...) function support different kinds of input data type.
Please notice they must be defined in a function package.

CREATE OR REPLACE PACKAGE <PKGNAME> AS
  FUNCTION MY_TOCHAR(PVALUE IN TIMESTAMP) RETURN VARCHAR2;
  FUNCTION MY_TOCHAR(PVALUE IN DATE, PFORMAT IN VARCHAR DEFAULT 'YYYY-MM-DD') RETURN VARCHAR2;
  FUNCTION MY_TOCHAR(PVALUE IN NUMBER, PRECISION IN INT DEFAULT 0, SCALE IN INT DEFAULT 0) RETURN VARCHAR2;
END;
/

CREATE OR REPLACE PACKAGE BODY <PKGNAME> AS
  FUNCTION MY_TOCHAR(PVALUE IN TIMESTAMP)
    RETURN VARCHAR2
  IS
  BEGIN
    RETURN TO_CHAR(PVALUE, 'YYYY-MM-DD-HH24.MI.SS.FF6');
  END;

  FUNCTION MY_TOCHAR(PVALUE IN DATE, PFORMAT IN VARCHAR)
    RETURN VARCHAR2
  IS
  BEGIN
    RETURN TO_CHAR(PVALUE, PFORMAT);
  END;

  FUNCTION MY_TOCHAR(PVALUE IN NUMBER, PRECISION IN INT, SCALE IN INT)
    RETURN VARCHAR2
  IS
    VFORMAT VARCHAR2(100);
  BEGIN
    IF PRECISION > 0 THEN
      VFORMAT := ...;
      IF SCALE > 0 THEN
        VFORMAT := ...;
      END IF;
      RETURN LTRIM(TO_CHAR(PVALUE, VFORMAT));
    ELSE
      RETURN TO_CHAR(PVALUE);
    END IF;
  END;

END;
/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值