如何定义功能
FIRST_WDOM()(星期/一个月的工作日)并
LAST_WDOM()采用样式
LAST_DAY()?
DELIMITER //CREATE FUNCTION first_wdom (d DATETIME) -- First work/week day of monthRETURNS DATE DETERMINISTICBEGIN DECLARE first_dom DATE; SET first_dom = DATE_FORMAT(d, '%Y-%m-01'); RETURN first_dom + INTERVAL (CASE DAYOFWEEK(first_dom) WHEN 1 THEN 1 WHEN 7 THEN 2 ELSE 0 END) DAY;END //CREATE FUNCTION last_wdom (d DATETIME) -- Last work/week day of monthRETURNS DATE DETERMINISTICBEGIN DECLARE last_dom DATE; SET last_dom = LAST_DAY(d); RETURN last_dom - INTERVAL (CASE DAYOFWEEK(last_dom) WHEN 1 THEN 2 WHEN 7 THEN 1 ELSE 0 END) DAY;END //DELIMITER ;mysql> SELECT FIRST_WDOM('2011-05-10'), LAST_WDOM('2011-05-10');+--------------------------+-------------------------+| FIRST_WDOM('2011-05-10') | LAST_WDOM('2011-05-10') |+--------------------------+-------------------------+| 2011-05-02 | 2011-05-31 | +--------------------------+-------------------------+1 row in set (0.01 sec)
这只是ic3b3rg答案的改编,并假定星期六和星期日为您的周末。我使用DATETIME而不是DATE作为输入类型,以避免传入时出现截断警告
NOW()。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)