Reputation: 345
I have list of datetime value. How to select previous year just for december only.
For example:
Current month = May 2016
Previous year of december = Dec 2015
(it will display data from dec 2015 to may 2016)
if Current month = May 2017
Previous year of december = Dec 2016 and so on.
(it will display data from dec 2015 to may 2016)
Any idea ? Thank you very much
Upvotes: 0
Views: 4841
Reputation: 444
SELECT
*
FROM
TableName
WHERE
TableName.Date BETWEEN CONVERT(DATE,CONVERT(VARCHAR,DATEPART(YYYY,GETDATE())-1)+'-12-'+'01') AND GETDATE()
Upvotes: 0
Reputation: 738
Below query will give the required output :-
declare @val as date='2016-05-19'
select concat(datename(MM,DATEADD(yy, DATEDIFF(yy,0,@val), -1)),' ',datepart(YYYY,DATEADD(yy, DATEDIFF(yy,0,@val), -1)))
output : december 2015
Upvotes: 1
Reputation: 1269443
You can subtract the month number:
select dateadd(month, - month(val), val)
Upvotes: 0