All of Query was successfully tested and running well on Microsoft SQL Server 2008

Kamis, 23 Agustus 2012

datediff in sql

declare @currDate datetime
declare @beforeDate datetime

set @currDate='2012-08-24 09:29:21.377'
set @beforeDate='2005-12-08 03:25:21.377'

select datediff(d,@beforeDate,@currDate)'days', 

datediff(mm,@beforeDate,@currDate)'months', datediff(yy,@beforeDate,@currDate)'years'

#Results:
days    months    years
2451    80        7


or can suggest like it

select (datediff(d,@beforeDate,@currDate))'difference',('Days')'unit' union
select datediff(mm,@beforeDate,@currDate),('Months') union
select datediff(yy,@beforeDate,@currDate),('Years')

#Results:
difference    unit
7             Years
80            Months
2451          Days

Notes:
datediff(d,@beforeDate,@currDate): to get difference of day
datediff(mm,@beforeDate,@currDate): to get difference of month
datediff(yy,@beforeDate,@currDate): to get difference of year

Tidak ada komentar:

Posting Komentar