您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL里求给定的时间是所在月份的第几个礼拜_MySQL

2024/6/11 14:15:50发布27次查看
bitscn.com
mysql里求给定的时间是所在月份的第几个礼拜
share 一个昨天写的函数。 目的是求给定的时间是所在月份的第几个礼拜。
[sql] 
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 7  
    when 2 then 1  
    when 3 then 2  
    when 4 then 3  
    when 5 then 4  
    when 6 then 5  
    when 7 then 6  
    end;  
    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
bitscn.com
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product