sql server - How to get date of previous year for a certain month and day... and vice versa for current year -
i wondering if assist me dilemma, have piece of sql has return amount in current time line, start date has 2014-03-01
, end date 2015-03-01
. tricky part should change every year automatically dont have change it, example next year should 2015-03-01
, 2016-03-01
.
the sql have written:
select c.name biller,sum(totaltransactions) totaltransactions, sum(totalpay@fee)/1.14 totalpay@fee tblpay@dailyretailerbillertotals d left outer join tblpay@company c (nolock) on d.clientid = c.recid name = 'name of biller' , datecreated >= 'start date' --2014-03-01 , datecreated < 'end date' -- 2015-03-01 group c.name order c.name asc
the following should give dates need:
select yearstart = cast(cast( datepart(year, getdate()) - case when datepart(month, getdate()) < 3 -1 else 0 end varchar(4)) + '0301' date), yearend = cast(cast( datepart(year, getdate()) - case when datepart(month, getdate()) < 3 0 else 1 end varchar(4)) + '0301' date);
the principle year, add '0301'
(1st march) date. need apply logic correct year, if date january of february, need deduct 1 year previous 1st march.
Comments
Post a Comment