Query SQL statements at the beginning and end of a quarter and at the beginning and end of the upper and lower quarters of a year

Source: Internet
Author: User
Query the SQL statement declare @ date & nbsp; datetimeset @ dategetdate () at the beginning of a quarter. Calculate the first day of the quarter where the given date is located, selectdateadd (quarter, datediff (quarter, 0, @ date), 0) as the first day of the current quarter-end of the quarter, calculate the last day of the quarter where the given date is located selectdate to query the end of the quarter, SQL statement at the beginning of the last quarter of the year

Declare @ date datetime
Set @ date = getdate ()
-- Calculate the first day of the quarter of the given date at the beginning of the quarter
Select dateadd (quarter, datediff (quarter, 0, @ date), 0) as 'first day of the current quarter'
-- Calculate the last day of the quarter of the given date at the end of the quarter
Select dateadd (quarter, 1 + datediff (quarter, 0, @ date),-1) as 'last day of the current quarter'
-- Beginning of last quarter
Select dateadd (quarter, datediff (quarter, 0, @ date)-1, 0) as 'beginning of the last quarter of the current quarter'
-- End of the last quarter
Select dateadd (quarter, datediff (quarter, 0, @ date),-1) as 'end of last quarter of the current quarter'
-- Beginning of next quarter
Select dateadd (quarter, 1 + datediff (quarter, 0, @ date), 0) as 'beginning of the next quarter of the current quarter'
-- End of next quarter
Select dateadd (quarter, 2 + datediff (quarter, 0, @ date),-1) as 'end of next quarter of the current quarter'
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.