SQL Server date is calculated
Tuesday, March 03, 2009 by rain
Normally,You need to win current date and consideration a few other date,For example,The first day when your program may need to judge a month or the last day.Your much National People's Congress knows how to have date breaking up without exception (year, month, day) ,Use merely next break up come out year, the month, day computation in be being put in a few function gives the date that he needs!In this article,My general tells you how to use DATEADD and DATEDIFF function to calculate a few different date that you should use the possibility in giving the order in you.
Before the example in use article,You must note the following problem.Major likelihood is not all example the outcome that carries out on different machine may be different,This is complete by the first days be a week which day this setting is decided.The first day (DATEFIRST) the first day when set decided which your system uses one day to serve as a week.All and the following example is with serving as a week the first day to build on Sunday,Namely the first day of setting is 7.If the first day of your setting is different,You may need to adjust these example,Make it and conform to of different the first day of setting close.You can check the first day setting through @@DATEFIRST function.
To understand these example,We review DATEDIFF and DATEADD function first.DATEDIFF function is calculated the hour between two date, day, week, month, year wait for time-interval gross.DATEADD function is calculated a date is decreased through giving time-interval to add will obtain a new date.Should understand more DATEDIFF and DATEADD function and time-interval to be able to read Microsoft online help.
Use DATEDIFF and DATEADD function will calculate date,The consideration method with the date that changes you to need from current date originally is a bit different.You must from time-interval this respect considers.For instance,There is how many time-interval between the date that should get to you from current date,Or,Arrive from today some day (for instance 1900-1-1) between have how many time-interval,Etc.Understand the different date computation example that how time-interval of with a view to conduces to you understanding me easily.
The first day of a month
The first example,My general tells you how to go from current date the last day of this month.Notice please:The other case in this example and this article will use DATEDIFF and DATEADD function to calculate only the date that we mean.Each example will pass computation but the time-interval before,Undertake adding decreasing will get meaning calculative date next.
This is the SQL that calculates the first days month Script:
SELECT DATEADD(mm, DATEDIFF(mm, 0, getdate()) , 0)
We part this statement will see it is how to work.Most the function of core is Getdate() ,The function that major person knows to this is to return current date and time.The function DATEDIFF(mm that the next carries out, 0, getdate()) is computation current date and " 1900-01-01 00:00:00 " the month between this date is counted.Remember:It is like period and time variable and millisecond from " 1900-01-01 00:00:00 " begin calculative.This is why you are OK expression of the first time is appointed to be in DATEDIFF function " 0 " .Next function is DATEADD,Increase current date to " 1900-01-01 " the month is counted.The date that defines beforehand through increasing " 1900-01-01 " with the lunar number of current date,The first day when we can acquire this month.Additional,The time part of the date that computation comes out will be " 00:00:00 " .
This calculative skill is to be calculated first current date arrives " 1900-01-01 " time-interval is counted,Add it next " 1900-01-01 " come up obtain special date,This skill can use calculation a lot of different date.Next case also is to use this skill to produce different date from current date.
Of this week on Monday
I am to use here (the time-interval of Wk) will calculate one day which is this week on Monday.
SELECT DATEADD(wk, DATEDIFF(wk, 0, getdate()) , 0)
The first day a year
Use now year (the first day when the time-interval of Yy) will show this one year.
SELECT DATEADD(yy, DATEDIFF(yy, 0, getdate()) , 0)
The first day of the quarter
If you want calculation the first day of this quarter,This example tells you how to should be done.
SELECT DATEADD(qq, DATEDIFF(qq, 0, getdate()) , 0)
The midnight that day
Once needed to pass Getdate() function to be worth cut to drop time share for release time,Can consider current date is in in the middle of the night.If such,The time that this example use DATEDIFF and DATEADD function will come to obtain midnight is nodded.
SELECT DATEADD(dd, DATEDIFF(dd, 0, getdate()) , 0)
Thorough DATEDIFF and DATEADD function are calculated
You can understand,Through using simple DATEDIFF and DATEADD function calculation,You can discover a lot of different may significant date.
At present all example till just are calculated merely present time and " 1900-01-01 " the time-interval amount between,Add it next " 1900-01-01 " computation will give date on time-interval.The amount that assumes you revise time-interval,Perhaps use different time-interval to transfer DATEADD function,Subtractive perhaps time-interval is not to increase,So the date that you can discover and differs more through these small adjustment.
There are four examples to use another DATEADD function to calculate here the last day will replace DATEADD function around respectively two time-interval.
The last day last month
This is a computation last month the example of the last day.It tells associate the last day of a month subtracts 3 millisecond will obtain on this example.One a little bit wants to remember,In Sql The time in Server is accurate to 3 millisecond.This is why the date that I need to subtract 3 millisecond will obtain me to want and time.
SELECT Dateadd(ms, - 3, DATEADD(mm, DATEDIFF(mm, 0, getdate()) , 0) )
The time part of the date that computation comes out included a Sql Last hour of a day when Server can record (" 23:59:59:997 " ) time.
The last day last year
The example above join,To want to get the last day last year,Your need subtracts in the first sky this year 3 millisecond.
SELECT Dateadd(ms, - 3, DATEADD(yy, DATEDIFF(yy, 0, getdate()) , 0) )
The last day this month
Now,To win the last day this month,I need to amend the statement that obtains the last day last month a little.Modification need gives the date before using DATEDIFF to be become quite and " 1900-01-01 " add on returned time-interval 1.Through increasing a month,The first day of next month of my plan cipher out,Subtract next 3 millisecond,The last day that gave this month with respect to computation so.This is computation this month the SQL script of the last day.
SELECT Dateadd(ms, - 3, DATEADD(mm, DATEDIFF(m, 0, getdate())+1, 0) )
The last day of the current year
You should master this practice now,This is script of the last day of computational the current year
SELECT Dateadd(ms, - 3, DATEADD(yy, DATEDIFF(yy, 0, getdate())+1, 0) )
The first of this month Monday
good,Now is the last example.I should be calculated here the first of this month Monday.This is calculative script.
Select DATEADD(wk, DATEDIFF(wk, 0, Dateadd(dd, 6-datepart(day, getdate()) , getdate())) , 0)
In this example,I was used " of this week on Monday " script,Made little revise.Modification part is an original scenario in " Getdate() " the part replaces computation the 6th day of this month,The first when replace current date make acquire this month computation is OKly in computation with the 6th day of this month Monday.
Summary
You give one a little bit when I hope these example can use DATEADD and date of DATEDIFF function computation in you inspire.Through using the mathematical method of the time-interval of this computation date,I discover to show removed useful calendar is valuable between two date.Notice,This is a kind of method that computation gives these date only.Should remember well,Still a lot of methods can get same computational result.If you have other methods,That is very pretty good,If you are done not have,I hope these example can give you a few inspire,Should use DATEADD and DATEDIFF function calculation when you when your program may want the date that uses.
---------------------------------------------------------------
Appendix,Other date processing technique
Second of time of 1) take out Declare @ Datetime Set @ = Getdate() - - '2003-7-1 10:00:00'
SELECT @ , DATEADD(day, DATEDIFF(day, 0, @) , 0)
2) indication week a few Select Datename(weekday, getdate() )
3) the day number that how acquires a certain months Declare @m Int Set @m=2 - - month Select Datediff(day, '2003-'+cast(@m As Varchar)+'-15' , '2003-'+cast(@m+1 As Varchar)+'-15' )
Additional,Obtain the day is counted this month Select Datediff(day, cast(month(GetDate() ) As Varchar)+'-'+cast(month(GetDate() ) As Varchar)+'-15' , cast(month(GetDate() ) As Varchar)+'-'+cast(month(GetDate())+1 As Varchar)+'-15' )
The script that perhaps makes maneuver calculates the last day this month,Use DAY function area next the last day
SELECT Day(dateadd(ms, - 3, DATEADD(mm, DATEDIFF(m, 0, getdate())+1, 0)) )
4) judge whether bissextile:
SELECT Case Day(dateadd(mm, 2, dateadd(ms, - 3, DATEADD(yy, DATEDIFF(yy, 0, getdate()) , 0))) ) When 28 Then ' of ' non-leap year Else ' bissextile ' End
Or Select Case Datediff(day, datename(year, getdate())+'-02-01' , dateadd(mm, 1, datename(year, getdate())+'-02-01') ) When 28 Then ' of ' non-leap year Else ' bissextile ' End
5) a quarter how many days Declare @m Tinyint, @time Smalldatetime Select @m=month(getdate() ) Select @m=case When @m Between 1 And 3 Then 1 When @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' Select Datediff(day, @time, dateadd(mm, 3, @time) )
...