Shabdar.org
Webshabdar.org
Calculate delay or difference between two dates in sql server PDF Print E-mail
Written by Shabdar   
Tuesday, 25 November 2008 12:07

DATEDIFF (Transact-SQL)

DATEDIFF ( datepart , startdate , enddate )
You can use SQL Server DateDiff function to calculate difference between two dates. This delay can be calculated in days, months, years, minutes,hours or seconds.

Here is documentation from MSDN
window.google_render_ad();
datepart

Is the parameter that specifies on which part of the date to calculate the difference. The following table lists dateparts and abbreviations recognized by SQL Server 2005. These dateparts and abbreviations cannot be supplied as a user-declared variable.

Datepart Abbreviations

year

yy, yyyy

quarter

qq, q

month

mm, m

dayofyear

dy, y

day

dd, d

week

wk, ww

Hour

hh

minute

mi, n

second

ss, s

millisecond

ms

startdate -

Is the starting date for the calculation. startdate is an expression that returns a datetime or smalldatetime value, or a character string in a date format.

Because smalldatetime is accurate only to the minute, when a smalldatetime value is used, seconds and milliseconds are always 0.

If you specify only the last two digits of the year, values less than or equal to the last two digits of the value of the two-digit year cutoff configuration option are in the same century as the cutoff year. Values greater than the last two digits of the value of this option are in the century that comes before the cutoff year. For example, if the two-digit year cutoff is 2049 (default), 49 is interpreted as 2049 and 2050 is interpreted as 1950. To avoid ambiguity, use four-digit years.

For more information about how to specify time values, see Time Formats. For more information about how to specify dates, see Date and Time (Transact-SQL).

enddate

Is the ending date for the calculation. enddate is an expression that returns a datetime or smalldatetime value, or a character string in a date format.

Return Types

integer

window.google_render_ad();
Examples

The following example determines the difference in days between the current date and the order date for products in the AdventureWorks database.

USE AdventureWorks;
GO
SELECT DATEDIFF(day, OrderDate, GETDATE()) AS NumberOfDays
FROM Sales.SalesOrderHeader;
GO

Comments/Questions

{mos_fb_discuss:19}
Comments
Add New Search
+/-
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
Please input the anti-spam code that you can read in the image.
Alexei  - Thanks |200.34.149.xxx |2009-05-11 16:04:36
Thanks, just what i was looking for
Nagendra  - Thank u |122.169.174.xxx |2009-05-29 15:48:53
:idea: :idea: :idea: :idea: :) :silly: :) :silly:

got what i wanted
Alex  - Thanks |98.174.232.xxx |2009-06-08 10:01:31
Just what I was looking for, nice tutorial
Bhavin  - Helpp please |203.77.192.xxx |2009-10-20 06:32:36
Haaii nice article but please help me in this problem.

I have two date dateto and datefrom i want to insert all the date between two in
one table with union of other table.

ex. 20/12/2009 bet 22/12/200 i want to insert 20/12/2009
21/12/2009
22/12/2009

reply in my mail if possible
thanks
Last Updated on Tuesday, 23 December 2008 11:27