Start date and end date are the dates to be compared. Date part is the part of date e g a year a quarter a month a week that you want to compare between the start date and end date see the valid date parts in the table below.
Datepart the units in which datediff reports the difference between the startdate and enddate commonly used datepart units include month or second.
How to get difference between two dates in sql. Here is the sql for this select from dt tb where dt between 2005 01 01 and 2005 12 31. Get difference in hours between 8 55 and 11 00 select datediff hh 08 55 11 00. The datediff function returns the difference between two dates.
Between two date ranges now let us move to select a range of records between two dates. They must be resolved to values of type date datetime datetimeoffset. In sql server you can use the t sql datediff function to return the difference between two dates times.
To calculate the difference between two dates you use the datediff function. It works on any expression that can be resolved to a time date smalldatetime datetime datetime2 or datetimeoffset value. Returns 3 although only 2 hours and 5 minutes passed between times get difference in months between sep 30 2011 and nov 02 2011 select datediff mm 2011 09 30 2011 11 02 returns 2 although only 1 month and 2 days passed between dates.
The following table lists all the valid datepart values datediff accepts either the full name of the datepart or any listed abbreviation of the. Date part start date and end date. To find the difference between two dates in the same column we need two dates from the same column.
Datediff datepart startdate enddate. The datediff function accepts three arguments. This article provides examples of the datediff function in sql server.
To calculate the difference between two dates in the same column we use the createddate column of the registration table and apply the datediff function on that column. The datepart value cannot be specified in a variable nor as a quoted string like month. The following illustrates the syntax of the datediff function in sql server.
In ms sql server the datediff function is used to get the difference between two dates in terms of years months days hours minutes etc.