Query which day is the first Monday of the month in which the given date is the SQL statement

Source: Internet
Author: User



DECLARE @date datetime;
Set @date = GETDATE ()
--Thinking, 1900-01-01 (Monday) plus (given a log of the month of 6th month and 1900-01-01 weeks) week
Why not choose number 7th? If it is number 7th, then 7 is exactly Sunday, the first Monday will be counted to 8th.
Why not choose number 5th? If number 5th is Saturday, Monday will be last month. Less than number 5th is the same as this reason.
Select DATEADD (Week,datediff (Week,0,dateadd day,6-datepart (Day, @date), @date)), 0) ' The first Monday ' of the Month
Select DATEADD (Week,datediff (Week,0,dateadd day,6-datepart (Day, @date), @date)), 7) ' The second Monday ' of the Month
Select DATEADD (Week,datediff (Week,0,dateadd day,6-datepart (Day, @date), @date)), 1) ' The first Tuesday ' of the Month
Select DATEADD (Week,datediff (Week,0,dateadd day,6-datepart (Day, @date), @date)), 8) ' The second Tuesday ' of the Month
Go

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.