share一个昨天写的函数。目的是求给定的时间是所在月份的第几个礼拜。delimiter$$use`t_girl`$$dropfunctionifexists`weekofmonth`$$createdefiner=`root`@`loca
share 一个昨天写的函数。 目的是求给定的时间是所在月份的第几个礼拜。
delimiter $$use `t_girl`$$drop function if exists `weekofmonth`$$create definer=`root`@`localhost` function `weekofmonth`(f_datetime datetime) returns int(11)begin-- created by ytt.declare v_result int;declare v_weekno int;set v_weekno =case dayofweek(f_datetime)when 1 then 7when 2 then 1when 3 then 2when 4 then 3when 5 then 4when 6 then 5when 7 then 6end;set v_result = ceil(dayofmonth(date_sub(f_datetime,interval v_weekno day))/7)+1;return v_result;end$$delimiter ;
演示下结果:
select weekofmonth(now()) as result;
query result:
result
4
本文出自 “上帝,咱们不见不散!” 博客,网站空间,美国空间,虚拟主机,请务必保留此出处