SQL Server Date Calculation (Favorites)

xiaoxiao2021-03-06  25

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. The first day of the first day of a month, I will tell you how to go from the last day of this month from the current 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 calculating the first day of the SQL script: select dateadd (mm, datediff (mm, 0), getDate ()), 0) 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. This week's Monday here I use week (WK) time intervals which day is Monday this week.

Select DateAdd (WK, Datediff (WK, 0, getDate ()), 0) The first day of the year (YY) is now used in the first day of the year. 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) The day of the day has needed to pass the getDate () function to cut off the time in order to return the time value, consider whether the current date is overnight. If this example, this example uses the dateDiff and dateadd functions to get the point in the middle of the night. Select DateAdd (DD, DD, 0, Getdate ()), 0) Deeper DateDiff and Dateadd Functions Calculate You can understand that by using simple dateDiff and dateadd functions, you can find a lot of different possible dates. 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) calculated date of the date of the date with a day of SQL Server can record ("23:59 : 59: 997 ") time. Last day, the last day of last year, in order to get the last day last year, you need to lose 3 milliseconds on the first day of this year. Select Dateadd (MS, -3, Dateadd (YY, Datediff (YY, 0)) The last day of this month is now, in order to get the last day of this month, I need to modify a lot of money. The last day of statement. 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 week of this month is good, now is 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 . Summary I hope that these examples can be inspired when you calculate the date with the 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.

-------------------------------------------------- ------------- Appendix, Other Date Treatment Method 1) Remove the timest second declare @ DateTime set @ = getdate () - '2003-7-1 10:00:00' select @, DATEADD (Day, Datediff (0, @), 0) 2) Show the week Select Datename (Weekday, getDate ()) 3) How to get a certain number of days in a month declare @m Int set @ m = 2 - month SELECT dateDiff (DAY, '2003 -' CAST (@M as varchar) '- 15', '2003 -' CAST (@ m 1 as varchar) '- 15') In addition, the number of days this month Select Datediff (day, cast (Month (getdate ()) as varchar) '-' casc (Month (Getdate ()) as varchar) '- 15', Cast (Month (getdate ()) as varchar) ' - ' CAST (Month (Getdate ()) 1 as varchar) ' - 15 ') or uses the last day of this month, then use the Day function area Select Day (DateAdd (MS, -3, Dateadd (mm, datediff (m, 0, getdate ()) 1, 0))) 4) Judgment whether the leap year: select Case Day (MM, 2, Dateadd (MS, -3, DateAdd (Yy, Datediff) YY, 0, Getdate (), 0))))))))))) WHEN 28 TEN 'ELSE' leap year 'end or Select Case Datediff (Day, Datename (Year, getDate ()) ' - 02-01 ', DateAdd ( MM, 1, Datename (Year, getDate ()) '- 02-01')) when 28 Ten ' Else 'leap year' end 5) How many days in a quarter Declare @m tinyint, @ Time SmallDatetime Select @ m = month ()) Select @ m = case when @m Between 1 and 3 Ten 1 by @m between 4 and 6 THEN 4 WHEN @M Between 7 and 9 THEN 7 else 10 End Select @ Time = Datename (Year, getDate ()) '-' Convert (varchar (10), @ m) '- 01'

转载请注明原文地址:https://www.9cbs.com/read-65099.html

New Post(0)