Typically, you need to get the current date and calculate some other dates, for example, your program may need to judge the first day or last day. Most of you know how to split the date (year, month, day, etc.), and then use only the annual, month, day, and the date you need! In this article, I will tell you how to use the Dateadd and Datediff functions to calculate some different dates that may be used in your program.
You must pay attention to the following questions before using the examples in this article. Most of these may not be all the results performed on different machines may not be the same, which is the first day of a week. This setting decision is determined. The first day (DateFirst) setting determines which day of your system uses the first day of the week. All of the following examples are established as a week on Sunday, which is set to 7 on the first day. If your first day setting is different, you may need to adjust these examples, making it in line with different first days. You can check the first day by @@ DateFirst function.
To understand these examples, let's review the Datediff and DateAdd functions first. The datediff function calculates the total number of hours, days, week, months, annual time intervals between two dates. The dateAdd function calculates a date by adding a date by giving time interval to get a new date. To learn more of the dateDiff and dateadd functions and time intervals, you can read Microsoft online help.
Use the DateDiff and DateAdd functions to calculate the date, and it is a bit different from the time you convert from the current date to the date you need. You have to consider this aspect from time interval. For example, how many time intervals from the date you have to get, or how many time intervals are there from today to a certain day (such as 1900-1-1), and so on. Understand how to focus on time interval helps you easily understand my different date calculations.
One month on the first day
In the first example, I will tell you how to go from the last day of this month from the date. Note: This example and other examples in this article will only use the dateDiff and dateadd functions to calculate the date we want. Each example will be calculated before the time interval, and then the addition or subtraction is made to get the date you want to calculate.
This is the first day of the first day: SELECT dateadd (mm, datediff (mm, 0, getdate ()), 0)
We separate this statement to see how it works. The core function is getDate (), most people know this is a function that returns the current date and time. The next executed function datediff (mm, 0, getdate ()) is the number of months between the current date and "1900-01-01 00: 00.000". Remember: The period and time variables are calculated from "1900-01-01 00:00:00.000". That's why you can specify the first time expression to "0" in the dateDiff function. The next function is dateadd, increasing the number of times the current date to "1900-01-01". We can get this month's first day by increasing the month of the predefined date "1900-01-01" and the current date. In addition, the calculated date of the date will be "00: 00: 00.000".
This calculation skill is to calculate the current date to "1900-01-01" number, then add it to "1900-01-01" to get a special date, this tip can be used to calculate many different dates . The next example is also a different date from the current date.
Monday this week
Here I am using weekly (WK) time intervals which day is this week's Monday.
Select DateAdd (WK, Datediff (WK, 0, Getdate ()), 0)
First day a year
Now use the first day of the year (YY).
Select Dateadd (YY, Datediff (YY, 0, Getdate ()), 0) The first day of the quarter
If you want to calculate the first day of this quarter, this example tells you how to do it.
Select Dateadd (QQ, Datediff (QQ, 0, Getdate ()), 0)
On the night of the day
Once needed to pass the getDate () function to cut off the time in order to return the time value, consider whether the current date is in half night. If this example, this example uses the dateDiff and dateadd functions to get the point in the middle of the night.
Select DateAdd (DD, Datediff (DD, 0, Getdate ()), 0) Deeper DateDiff and DateAdd Function
You can understand that you can find a lot of different possible dates by using a simple dateDiff and dateadd function.
All examples thereof only calculate only the current time and the number of time intervals between "1900-01-01", and then add it to the "1900-01-01" time interval to calculate the date. Assume that you modify the number of time intervals, or use a different time interval to call the DateAdd function, or subtract time intervals instead of increasing, you can discover and make differences through these small adjustments.
Here are four examples of using another DATEADD function to calculate two time intervals before and after the dateadd function in the last day.
The last day of last month
This is an example of calculating the last day of last month. It is obtained by minus 3 milliseconds from the last day of one month. One thing to remember, the time in SQL Server is accurate to 3 milliseconds. That's why I need to lose 3 milliseconds to get the date and time I want.
Select Dateadd (MS, -3, DateAdd (mm, datediff (mm, 0, getdate ()), 0))
The calculated date of the calculated date contains a time of SQL Server to record ("23: 59: 59: 997") time.
Last day of last year
Connect the above example, in order to get the last day of last year, you need to lose 3 milliseconds on the first day of this year.
Select Dateadd (MS, -3, Dateadd (Yy, Datediff (YY, 0, Getdate ()), 0))
The last day of this month
Now, in order to get the last day of this month, I need a slight modification to get the last day of last month. Modification needs to be added to the time interval returned by Datediff and "1900-01-01". By adding 1 month, I calculated the first day of the next month, then minus 3 milliseconds, which calculated this month's last day. This is the SQL script that calculates the last day of this month.
Select Dateadd (MS, -3, dateAdd (mm, datediff (m, 0, getdate ()) 1, 0))
The last day of this year
You should now master this practice, this is the last day of this year.
Select Dateadd (MS, -3, dateAdd (YY, Datediff (YY, 0, getDate ()) 1, 0)).
The first Monday this month
Ok, it is now the last example. Here I am going to calculate the first Monday of this month. This is the calculated script. Select Dateadd (WK, Datediff (WK, DatePart (DD (DD, 6-datepart ()), getdate ())), 0) In this example, I used "Monday this week" Script and make a little modification. The modified part is to replace the "getDate ()" section in the original script to calculate this month, replacing the current date in the calculation of this month, make the calculation to get this month's first Monday .
to sum up
I hope that these examples can be inspired when you calculate the date with Dateadd and Datediff functions. By using this time interval of this calculation date, I found that useful calendar in order to display the intervals between the two dates. Note that this is just a method of calculating these dates. It is necessary to keep in mind, there are many ways to get the same calculation results. If you have other methods, it is very good, if you don't, I hope these examples can give you some inspiration, when you want to use the Dateadd and Datediff function to calculate the date you may use when you can use.