Calculate the 2nd days of the week of the given date (Sunday is the first day) SQL statement

Source: Internet
Author: User
Declare @ date & nbsp; datetimeset @ dategetdate (): Current date + Monday (2nd days per week) the number of days difference from the current date -- datepart (weekday, date) the returned value is related to @ datefirst setdatefirst7 -- or set it to the American English setlanguage to calculate the 2nd day of the week of the given date (Sunday is the first day)

Declare @ date datetime
Set @ date = getdate ()
-- Train of thought: the number of days between the current date + Monday (2nd days per week) and the current date
-- The return value of datepart (weekday, date) is related to @ datefirst.
Set datefirst 7 -- or set it to set language us_english; (Sunday is the First Day)
Select dateadd (day, 2-datepart (weekday, @ date), @ date) as 'Day of the week, Monday'
-- Monday is irrelevant to the SQL server language version or @ datefirst
-- '2014-01-01 'is Monday, '2014-01-01' plus (number of weeks between the current date and)
Select dateadd (week, datediff (week, 0, @ date), 0) as 'Monday of the week'
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.