Query SQL statements at the beginning of the year, at the end of last year, at the beginning of next year, and at the end of next year

Source: Internet
Author: User
Query SQL statements at the beginning of the year, at the end of last year, at the beginning of next year, and at the end of next year

Query SQL statements at the beginning of the year, at the end of last year, at the beginning of next year, and at the end of next year

Query SQL statements at the beginning of the year, at the end of last year, at the beginning of next year, and at the end of next year
-- Annual computing

Declare @ date datetime
Set @ date = getdate ()
-- Calculate the first day of the year of the given date at the beginning of the year
Select dateadd (year, datediff (year, 0, @ date), 0) as 'Day of the year'
-- Calculate the last day of the year of the given date at the end of the year.
Select dateadd (year, datediff (year,-1, @ date),-1) as 'last day of the year'
-- Calculate the first day of the year in which the given date is located at the beginning of the previous year.
Select dateadd (year, datediff (year,-0, @ date)-) as 'Day of the previous year of the year'
-- Calculate the last day of the year at the end of the previous year
Select dateadd (year, datediff (year, 0, @ date),-1) as 'the last day of the previous year'
-- Calculate the first day of the next year in the year of the given date.
Select dateadd (year, 1 + datediff (year, 0, @ date), 0) as 'Day of the next year of the year'
-- Calculate the last day of the next year at the end of the next year
Select dateadd (year, 1 + datediff (year,-1, @ date),-1) as 'the last day of the next year'
Go
Related Article

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.