In MySQL, the given time is the nth week of the month.

Source: Internet
Author: User

In MySQL, the given time is used to Share a function written yesterday in the nth week of the month. The purpose is to find that the given time is the nth week of the month. [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; demo result: SELECT weekofmonth (NOW () AS result; query result: result4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.