How to get SQL first day of the quarter, the first day of the year, the last day of the month

nterval parameters, with the following settings:

  Setting Description

  Year yy, yyyy 年

  quarter qq, q season

  Month mm, m 月

  dayofyear dy, y one year specific number of days

  Day dd, d 日

  Week wk, ww day of the week number

  Hour hh hours

  minute mi, n min

  second ss, s seconds
millisecond ms ms

  ① Monday of this week,
  
  here I am with a time interval to calculate the week (wk) which day of the week is Monday.
  
  DATEADD the SELECT (wk, DATEDIFF (wk, 0, getdate ()), 0)
  
  ② first day of the year
  
  now in years (yy) time interval to display the first day of the year.
  
  SELECT DATEADD (yy, DATEDIFF (yy , 0, getdate ()), 0)
  
  on the first day of the quarter ③
  
  If you want to calculate the first day of the quarter, this example tells you what to do.
  
  DATEADD the SELECT (QQ, DATEDIFF (QQ, 0, getdate ()), 0)
  
  ④ midnight the day
  
  ever needed () function returns the time value to cut off part time, will take into account the current date is not in the middle of the night by getdate. If so, this example makes
use DATEDIFF DATEADD function and obtains a time point at midnight.
  
  SELECT DATEADD (dd, DATEDIFF (dd , 0, getdate ()), 0)
  
  in depth and DATEDIFF function calculates DATEADD
  
  you can see, is calculated by using a simple and DATEADD DATEDIFF function, you can find a lot of different possible meaningful date.
  
  All the examples so far is only the number of time interval is calculated only between the current time and "1900-01-01", and then add it to "1900-01-01" in
the time interval up to calculate the date. Assume you change the number of time intervals, or use different time intervals to call DATEADD function, or subtract time interval
Not increase, and then you can see many different dates through these small adjustments.
  
  Here is an example of the use of four additional function to calculate a DATEADD last day intervals before and after the two were replaced DATEADD function.

  ⑤ the last day of last month's
  
  example of this is the last day of a month is calculated. It is obtained by subtracting 3 milliseconds from the last day of the month in this example. One thing to keep in mind, in Sql
Server time is accurate to 3 milliseconds. That's why I need to subtract 3 milliseconds to get the date and time I want.
  
  SELECT dateadd (ms, -3, DATEADD (mm, DATEDIFF (mm, 0, getdate ()), 0))
  
  time calculated from the date portion contains a Sql Server may record the last moment of the day ( "23:59 : 59: 997 ") of the time.
  
  ⑥ the last day of last year's
  
  example of the connection above, in order to get the final day of last year, you need to subtract 3 milliseconds in the first day of the year.
  
  DATEADD the SELECT (MS, -3, DATEADD (YY, DATEDIFF (YY, 0, getdate ()), 0))
  
  ⑦ last day of the month
  
  now, in order to obtain the last day of this month, I need a little tinkering to get the last the last day of the month sentence. Comparing the current with changes need to DATEDIFF
date and "1900-01-01" return interval time plus 1. By adding a month, I calculate the first day of the next month, then subtract 3 milliseconds, thus calculated out of this
last day of the month. This is calculated on the last day of this month SQL scripts.
  
  SELECT dateadd (ms, -3, DATEADD (mm, DATEDIFF (m, 0, getdate ()) + 1, 0))

  ⑧ last day of the year
  
  you should now grasp this approach, which is to calculate the last day of the year script
  
  SELECT dateadd (ms, -3, DATEADD (yy, DATEDIFF (yy, 0, getdate ()) + 1, 0)) .
  
  The first Monday of the month ⑨
  
  Well, now is the last example. Here I want to calculate the first Monday of the month. This is the script calculation.
  
  select DATEADD (wk, DATEDIFF (wk , 0, dateadd (dd, 6-datepart (day, getdate ()), getdate ())), 0)
  
  In this example, I used the "Monday of this week," the script and made a little modification. Modified part is the original script "getdate ()" section
replaced calculate the 6th day of the month, in the first day of the month is calculated by 6 to replace the current date so that the calculation can be obtained on the first Monday of the month .

Reproduced in: https: //www.cnblogs.com/kevinGao/p/3589959.html

Guess you like

Origin blog.csdn.net/weixin_34391445/article/details/93373443