0919MYSQL take the first and last day of the current week/month/quarter/year

Source: Internet
Author: User

http://blog.csdn.net/cleanfield/article/details/41447585

Compiled SQL code, all executable

  1. #当年第一天:
  2. SELECT date_sub (Curdate (), INTERVAL DayOfYear (Now ()) – 1 day );
  3. #当年最后一天:
  4. SELECT concat (now()),'-12-31 ');
  5. #当前week的第一天:
  6. Select Date_sub (Curdate (), INTERVAL WEEKDAY (Curdate ()) + 1 day );
  7. #当前week的最后一天:
  8. Select Date_sub (Curdate (), INTERVAL WEEKDAY (Curdate ())-5 day );
  9. #前一week的第一天:
  10. Select Date_sub (Curdate (), INTERVAL WEEKDAY (Curdate ()) + 8 day );
  11. #前一week的最后一天:
  12. Select Date_sub (Curdate (), INTERVAL WEEKDAY (Curdate ()) + 2 day );
  13. #前两week的第一天:
  14. Select Date_sub (Curdate (), INTERVAL WEEKDAY (Curdate ()) + day );
  15. #前两week的最后一天:
  16. Select Date_sub (Curdate (), INTERVAL WEEKDAY (Curdate ()) + 9 day );
  17. #当前The first day of month:
  18. SELECT concat (Date_format (Last_day (now ()),'%y-%m-'),' 01 ');
  19. #当前The last day of month:
  20. SELECT Last_day (now ());
  21. #前一The first day of month:
  22. SELECT concat (Date_format (Last_day (now ()-Interval 1 month), '%y-%m-'),' 01 ');
  23. #前一The last day of month:
  24. SELECT Last_day (now ()-Interval 1 month);
  25. #前两The first day of month:
  26. SELECT concat (Date_format (Last_day (now ()-Interval 2 month), '%y-%m-'),' 01 ');
  27. #前两The last day of month:
  28. SELECT Last_day (now ()-Interval 2 month);
  29. #当前quarter的第一天:
  30. Select Concat (Date_format (Last_day (Makedate (EXTRACT (year from curdate ()), 1) + Interval QUARTER (curdate ()) *   3-3 month), '%y-%m-'),' 01 ');
  31. #当前quarter的最后一天:
  32. Select Last_day (Makedate (EXTRACT (year from curdate ()), 1) + Interval QUARTER (curdate ()) *3-1 month);
  33. #前一quarter的第一天:
  34. Select Concat (Date_format (Last_day (Makedate (EXTRACT (year from curdate ()), 1) + Interval QUARTER (curdate ()) *  3-6 month), '%y-%m-'),' 01 ');
  35. #前一quarter的最后一天:
  36. Select Last_day (Makedate (EXTRACT (year from curdate ()), 1) + Interval QUARTER (curdate ()) *3-4 month);
  37. #前两quarter的第一天:
  38. Select Concat (Date_format (Last_day (Makedate (EXTRACT (year from curdate ()), 1) + Interval QUARTER (curdate ()) *  3-9 month), '%y-%m-'),' 01 ');
  39. #前两quarter的最后一天:
  40. Select Last_day (Makedate (EXTRACT (year from curdate ()), 1) + Interval QUARTER (curdate ()) *3-7 month);

0919MYSQL take the first and last day of the current week/month/quarter/year

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.