Date calculation methods commonly used in reports (personal summary)

Source: Internet
Author: User

--------------------------------------------

 

-- Title: the commonly used date calculation method in reports

 

-- Author: htl258 (Tony)

 

-- Date:-09-21

 

-- Reprinted please keep this information

 

--------------------------------------------

 

Declare @ DT datetime

Set @ dt = '2017-09-20'

-- 1.1 calculate the day of the month:

Select dateadd (mm, datediff (mm, 0, @ DT), 0) -- 00:00:00. 000

-- 1.2 calculate the last day of the month:

Select dateadd (mm, datediff (mm, 0, @ DT) +)-1 -- 00:00:00. 000

-- 2.1.1 calculate the first day of the week (Sunday is the first day): Two Methods

Select dateadd (wk, datediff (wk, 0, @ DT), 0)-1 -- 00:00:00. 000

Select @ DT + 1-datepart (W, @ DT + @ datefirst) -- 00:00:00. 000

-- 2.1.2 calculate the first day of the week (Monday is the first day ):

Select @ DT + 1-datepart (W, @ DT + @ datefirst-1) -- 00:00:00. 000

-- 2.2 calculate the last day of the week (based on the first day of the week)

-- 2.3.1 calculate the first business day of the week (Sunday is the first day ):

Select dateadd (wk, datediff (wk, 0, @ DT), 0) -- 00:00:00. 000

Select @ DT + 2-datepart (W, @ DT + @ datefirst) -- 00:00:00. 000

Select dateadd (DD, 1-(datepart (W, @ DT) + @ datefirst-1) % 7, @ DT) -- 00:00:00. 000

-- 2.3.2 calculate the first business day of the week (Monday is the first day ):

Select @ DT + 1-datepart (W, @ DT + @ datefirst-1) -- 00:00:00. 000

-- 2.4 calculate the last workday of the Week (based on the first workday)

-- 2.5 calculate the first Monday of the month:

Select dateadd (wk, datediff (wk, 0, dateadd (DD, 6-datepart (DD, @ DT), @ DT), 0)

-- 2009-09-07 00:00:00. 000

-- 3.1 The first day of the current year:

Select dateadd (YY, datediff (YY, 0, @ DT), 0) -- 00:00:00. 000

-- 3.2 the last day of the year:

Select dateadd (YY, datediff (YY, 0, @ DT) +)-1 -- 00:00:00. 000

-- 4.1 the first day of this quarter:

Select dateadd (QQ, datediff (QQ, 0, @ DT), 0) -- 00:00:00. 000

-- 4.2 request the last day of this quarter:

Select dateadd (QQ, datediff (QQ, 0, @ DT) +)-1 -- 00:00:00. 000

-- 5.1 calculate the first day of the semi-year:

Select dateadd (mm, datediff (mm, 0, dateadd (mm,-(month (@ DT)-1) % 6, @ DT), 0)

-- 00:00:00. 000

-- 5.2 calculate the last day of the semi-year:

Select dateadd (mm, datediff (mm, 0, dateadd (mm,-(month (@ DT)-1) % 6 + 6, @ DT), 0)-1

-- 00:00:00. 000

 

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/htl258/archive/2009/09/21/4574401.aspx

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.