DATEDIFF functions for SQL Server

Source: Internet
Author: User
Tags date1

These two days to change a SQL Server database program to Oracle, found that two of the functions between the database are many different. The DateDiff function usage in SQL Server's database is explained as follows:

Describe
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. See the Settings section for values.
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. See the Settings section for values.
FirstWeekOfYear is optional. A constant that specifies the first week of the year. If not specified, the default is the week in which January 1 is located. See the Settings section for values.


Set up
The interval parameter can have the following values:
Setting description
YYYY year
Q Quarter
M-month
Y number of days in a year
D-Day
W The number of days in a week
WW Week
H hours
M min
s S


The FirstDayOfWeek parameter can have the following values:

Constant Value Description
Vbusesystem 0 uses the regional language Support (NLS) API settings.
Vbsunday 1 Sunday (default)
Vbmonday 2 weeks A
Vbtuesday 3 Week II
Vbwednesday 4 week Three
Vbthursday 5 Thu
Vbfriday 6 Friday
Vbsaturday 7 Saturday


The FirstWeekOfYear parameter can have the following values:

Constant Value Description
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.


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.

Related Article

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.