SQL Server DATEDIFF() function

Returns the time interval between two dates. Syntax DateDiff(interval, date1, date2 [,firstdayofweek][, firstweekofyear]]) The syntax of the DateDiff function has the following parameters: 


Parameter description interval Required. A string expression that is used to calculate the time interval between date1 and date2 date1, date2 is required. date expression. The two dates used for the calculation. firstdayofweek Optional. A constant specifying the first day of the week. If not specified, defaults to Sunday firstweekofyear Optional. A constant specifying the first week of the year. If not specified, it defaults to the week of January 1st  

 


1. The interval parameter can have the following values:

datepart abbreviation
year yy, yyyy
quarter qq, q
moon mm, m
day of the year dy, y
day dd, d
week wk, ww
Week dw, w
Hour hh
minute my, n
Second ss, s
millisecond ms
subtle mcs
nanosecond ns

 

 

 

 

 

 

 

 

 

 

 

 

firstdayofweek parameter:

vbUseSystem 0 Use the National Language Support (NLS) API setting. vbSunday 1 Sunday (default) vbMonday 2 Monday vbTuesday 3 Tuesday vbWednesday 4 Wednesday vbThursday 5 Thursday vbFriday 6 Friday vbSaturday 7 Saturday 
 
 
 
 
 
 
 

firstweekofyear Parameters:
vbUseSystem 0 Use the Regional Language Support (NLS) API setting. vbFirstJan1 1 Starts the week of January 1st (default). vbFirstFourDays 2 starts with the first week of the new year that has at least four days. vbFirstFullWeek 3 starts with the first full week in the new year. 
 
 

 

Example: SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate returns 1

Description
The DateDiff function is used to determine the number of specified time intervals that exist between two dates. For example, you can use DateDiff to calculate the number of days between two dates, or the number of weeks between the current day and the last day of the year.
To calculate the number of days between date1 and date2, use "days in a year" ("y") or "days" ("d"). When interval is "days of the week" ("w"), DateDiff returns the number of weeks between two dates. If date1 is a Monday, DateDiff counts up to the number of Mondays before date2. This result contains date2 but not date1. If interval is "week" ("ww"), the DateDiff function returns the number of weeks between two dates in the calendar table. The function counts the number of Sundays between date1 and date2. DateDiff will calculate date2 if date2 is Sunday, but will not calculate date1 even if date1 is Sunday.

The DateDiff function returns a negative number if date1 is later than date2.

The firstdayofweek parameter affects calculations using the "w" and "ww" interval notation.

If date1 or date2 is a date literal, the specified year becomes a fixed part of the date. But if date1 or date2 is enclosed in quotation marks (" ") and the year is omitted, the current year is inserted every time the date1 or date2 expression is evaluated in the code. This makes it possible to write program code for different years.

When the interval is "year" ("yyyy"), compare December 31st to January 1st of the following year, although the difference is actually only one day, DateDiff returns 1 to indicate a difference of one year.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970593&siteId=291194637