lamer lamer
lamer lamer

Reputation: 345

how to get previous year (month december) and month showing current month in sql

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

Answers (3)

mindbdev
mindbdev

Reputation: 444

SELECT 
*
FROM
TableName
WHERE
TableName.Date BETWEEN CONVERT(DATE,CONVERT(VARCHAR,DATEPART(YYYY,GETDATE())-1)+'-12-'+'01') AND GETDATE()

Upvotes: 0

Ragesh
Ragesh

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

Gordon Linoff
Gordon Linoff

Reputation: 1269443

You can subtract the month number:

select dateadd(month, - month(val), val)

Upvotes: 0

Related Questions