Wednesday, 18 January 2017

Sql date difference in years months and days


//// Method 1

DECLARE @getmm INT,@yy int ,@mm int,@dd int,@getdd INT,@dstart datetime='2010-01-01',@dend datetime='2016-06-12'

SET @yy = DATEDIFF(yy, @dstart, @dend)
SET @mm = DATEDIFF(mm, @dstart, @dend)
SET @dd = DATEDIFF(dd, @dstart, @dend)
SET @getmm = ABS(DATEDIFF(mm, DATEADD(yy, @yy, @dstart), @dend))
SET @getdd = ABS(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, @yy, @dstart), @dend), DATEADD(yy, @yy, @dstart)), @dend))

select 'Year : '+ Convert(varchar(10),@yy) + ',    ' +'Month : '+ Convert(varchar(10),@getmm) + ',    '  + 'Days : '+Convert(varchar(10),@getdd)

/// Result


No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...