Oracle和mysql自定义函数

场景描述:根据要求判断当前时间属于那一天的工作量,如10:00:00之前就是前一天的工作量。

oracle

create or replace function get_date(v_datetime in date,v_time in varchar2) return varchar2 as

  v_date varchar2(50);
  beginDate varchar2(50);
  cDate varchar2(50);
begin
   cDate:= to_char(v_datetime, 'YYYY-MM-DD hh24:mi:ss');
   beginDate:=concat(to_char(v_datetime, 'YYYY-MM-DD '),v_time);
   if cDate<beginDate then
     v_date:=to_char(v_datetime-1, 'YYYY-MM-DD');
     else
       v_date:=to_char(v_datetime, 'YYYY-MM-DD');
   end if;
   return v_date;
exception
  when no_data_found then
    raise_application_error(-20001, '你输入的日期无效!');
end get_date;

示例

 select get_date(sysdate,'10:00:00') from dual

mysql

DROP FUNCTION IF EXISTS get_date;
CREATE FUNCTION get_date(v_datetime datetime,v_time VARCHAR(50)) 
RETURNS VARCHAR(50)
BEGIN
	DECLARE v_date VARCHAR(50);
  DECLARE cDate VARCHAR(50);
  DECLARE beginDate VARCHAR(50);
	set cDate= date_format(v_datetime, '%Y-%m-%d %H:%i:%s'); 
	set beginDate=concat(date_format(v_datetime, '%Y-%m-%d '),v_time); 
	if cDate<beginDate then
     set v_date=date_format(date_add(v_datetime,interval -1 day), '%Y-%m-%d');
   else
     set  v_date=date_format(v_datetime, '%Y-%m-%d');
  end if;
	RETURN v_date;
END

示例

select get_date(now(),'10:00:00')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值