SQL Server DATEDIFF () function

Source: Internet
Author: User
Tags date1

Returns a time interval between two dates.
Grammar
DateDiff (interval, date1, date2 [, firstdayofweek][, FirstWeekOfYear]])
The syntax for the DATEDIFF function has the following parameters:

Parameter Description
Interval must be selected. A string expression that is used to calculate the time interval between Date1 and Date2
Date1, Date2 must be selected. The date expression. The two dates used for the calculation.
FirstDayOfWeek is optional. Constant that specifies the first day of the week. If not specified, the default is Sunday
FirstWeekOfYear is optional. A constant that specifies the first week of the year. If not specified, the default is the week of January 1

The 1.interval parameter can have the following values:

DatePart Abbreviations
Years YY, yyyy
Quarter QQ, Q
Month MM, M
The day of the year Dy, y
Day DD, D
Week WK, WW
Week DW, W
Hours hh
Minutes MI, n
Seconds SS, S
Milliseconds Ms
Subtle MCs
Na-Sec Ns

FirstDayOfWeek Parameters:

vbusesystem 0 uses the regional language Support (NLS) API settings.  
Vbsunday 1 Sunday (default)  
Vbmonday 2 weeks a  
Vbtuesday 3 Tuesday  
Vbwednesday 4 weeks three  
Vbthursday 5 Thu  
Vbfriday 6 Friday  
Vbsaturday 7 Saturday  

FirstWeekOfYear Parameters:
Vbusesystem 0 uses the regional language Support (NLS) API settings.
VbFirstJan1 1 starts from the week of January 1 (default).
vbFirstFourDays 2 starts with the first week of at least four days in the new year.
Vbfirstfullweek 3 starts with the first full week in the new year.

Example 1:

SELECT DATEDIFF (Day, ' 2008-12-29 ', ' 2008-12-30 ') as Diffdate returns 1

Example 2:

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >  Select COUNT (1) as Num from T_ordercar OC left joins T_order O on OC. OrderID = o.id
   where   datediff (hh, ' 2017-12-19 8:00am ', OC. Addtime) >= 0  and DateDiff (Hh,oc. Addtime, ' 2017-12-22 9:00am ') >0

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 that differ by two dates, or the day of the week between the last day of the year.
To calculate the number of days between Date1 and Date2, you can use the days of the year ("Y") or the "Day" ("D"). When interval is "Days of the Week" ("W"), DateDiff returns the number of weeks between two dates. If Date1 is Monday, DateDiff calculates the number of Monday before Date2. This result contains date2 and does not contain date1. If interval is "Week" ("WW"), the DateDiff function returns the number of weeks between two dates in the Calendar table. The function calculates the number of Sunday between Date1 and Date2. If Date2 is Sunday, DateDiff calculates date2, but even if Date1 is Sunday, date1 is not counted.

If Date1 is later than Date2, the DateDiff function returns a negative number.

The FirstDayOfWeek parameter has an effect on the calculations that use the "w" and "ww" interval symbols.

If Date1 or date2 is a date literal, the specified year becomes a fixed part of the date. However, if Date1 or date2 is included in quotation marks ("") and the year is omitted, the current year is inserted each time the Date1 or date2 expression is evaluated in the code. This allows you to write program code that applies to different years.

When the interval is "year" ("yyyy"), compare December 31 and the next year January 1, although actually only one day, DateDiff returns 1 indicates a difference of one year.

SQL Server DATEDIFF () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.