Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with DATEDIFF () function in SQL Server 2008 using PL/SQL.

Author: Shehzad Hemani

In this simple article you will learn that how to work with DATEDIFF () function in SQL Server using PL/SQL.

 DATEDIFF () is the one of the most commonly used functions of SQL Server. It is use to find out the difference between the two DateTime elements. This function takes three parameters; one is the date part to specify which part of the date is required, second and third are two dates’ variable from which difference has to be calculated.

 

Syntax:

DATEDIFF (datepart, firstdate, seconddate)

 

It can be used only with the SELECT command. The syntax for using it with SELECT command

 

SELECT

DATEDIFF(datepart, firstdate, seconddate)

AS

Header of Column

 

Example:

 

declare two datetime variable:

 

Declare @Date1 datetime

Declare @Date2 datetime

 

To set date1 with current Date:

 

Set @Date1 = (SELECT DATE)

 

And to set Date2 with the date after ten days of the first one

 

Set @Date2 = (SELECT DATEADD(day, 10,@Date1 ))

 

Where DATEADD is the function to add a specific number of days to a specific date

Now to get the difference between above declared DateTime variables

 

SELECT

DATEDIFF (day, @Date1, @Date2)

AS

Difference Of Days

 

 Output:

 

Difference Of Days

-------------

5

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with keyword NOLOCK in SQL server 2008

Disclaimer - Privacy
© 2002-2017 DevASP.net