Query the SQL statement of the last Sunday (Sunday is the first day) of the week of the given date.

Source: Internet
Author: User
Query the last Sunday of the week of the given date (Sunday is the first day) SQL statement declare @ date & nbsp; datetimeset @ dategetdate () -- idea: the day of the week where the current log is located minus one week -- datepart (weekday, date) the returned value is related to @ datefirst setdatefirst7 -- or the SQL statement used to query the last Sunday (Sunday is the first day) of the week on which the given date is located.

Declare @ date datetime
Set @ date = getdate ()
-- Train of thought: minus one week on Sunday of the week where the current log is located
-- 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 (week,-1, dateadd (day, 1-datepart (weekday, @ date), @ date) as 'Day of last week, Sunday'
-- One week equals 7 days
Select dateadd (day,-7, dateadd (day, 1-datepart (weekday, @ date), @ date) as 'Day of last week, Sunday'
-- Simplified
Select dateadd (day,-6-datepart (weekday, @ date), @ date) as 'Day of last week, Sunday'
-- Last Sunday, it has nothing to do with the SQL server language version or @ datefirst
Select dateadd (week,-1 + datediff (week,-1, @ date),-1) as 'last Sunday'
-- Or
Select dateadd (week, datediff (week, 6, @ date),-1) as 'last Sunday'
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.