Wednesday 12 June 2013

datediff in sql

Structured Query Language Provides Several Functions to perform date and time related operations. These functions allow you to add dates , times. To Find difference between dates and time etc.

1. CurrentDate :- Current Date is used to display current date according to date set in computer system.


Example :-
Select getdate ( ) as currentdate



2. Date Diff :- Date Diff function is used to find difference between two specified dates . 

SYNTAX :- 
DATEDIFF ( date-part , start-date , end-date )
* Date-part - It specifies what type of difference you want to find 
between two specified 
dates.
* Start-date - It is starting date from two dates to find difference 
of.
* end-date - It is ending date from two dates to find difference of.


DAY-DIFFERENCE

Query :- select datediff ( d, '2013-1-1' , 2013-7-2' ) as"DateDiff"


Month-DIFFERENCE

Query :- 
select datediff ( month, '2013-1-1' , 2013-7-2' ) as"Date Diff"



Week-DIFFERENCE

Query :- 


select datediff ( week, '2013-1-1' , 2013-7-2' ) as"Date Diff"


Year-DIFFERENCE

Query :- 


select datediff ( year, '2013-1-1' , 2013-7-2' ) as"Date Diff"



Hour-DIFFERENCE

Query :- 


select datediff ( hour, '2013-1-1' , 2013-7-2' ) as"Date Diff"




Minute-DIFFERENCE

Query :- 


select datediff ( minute, '2013-1-1' , 2013-7-2' ) as"Date Diff"



Second-DIFFERENCE

Query :- 


select datediff ( second, '2013-1-1' , 2013-7-2' ) as"Date Diff"




Share this

0 Comment to "datediff in sql"

Post a Comment