DateDiff function in SQL Server

March 17, 2009 at 3:13 pm (SQL, Technical, Tips and Tricks) (, )

I frequently used to calculate date difference between two dates. I use to open the calendar and calculate the difference. But now I have found a easier way to calculate the date difference using the SQL DateDiff function

DateDiff returns the number of date and time boundaries crossed between two dates.

Syntax:

DateDiff(datepart, startdate, enddate)

where datepart is the parameter that specifies which part of the date to calculate difference.

datepart can be any one in the following

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

Example:

select datediff(“d”,’03/10/2009′,’03/17/2009′)

will return you 7.

For more details, you can view here

Advertisements

4 Comments

  1. ss said,

    I tested but not working..in sql 2008

  2. Shijo Joseph said,

    hi ss,

    Check with following

    select DATEDIFF(DAY,’01-01-2007′,’01-01-2008′)

    This will work
    Tctcworld.com

  3. SQL Server Helper said,

  4. na said,

    ok

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: