sql - Generate records on interval of time (Weekly, Monthly, Quarterly, Yearly) -
I need to generate a record on the given start date, end-date and time-frequency (weekly, monthly etc.)
February 1 - 28 February (29 leap years)
March 1 - 31 March
. .
- 4 - December 1 - December 31
Example - 2
- Start
- Expiry date: 31 December 2011
- Frequency: Weekly
Reports generated:
-
2 - 17 Jan - 23 January
-
3 24 January - 30 January
.
- 50- 26 December 31 December
Also in the form of quarterly.
Any help
I think. Are you asking how the loop is basically months / weeks / quarters?
It should be given that you hope you have a good start to point you in the right direction. All you have to do is just copy the SQL date functions such as
DATEADD, DATEDIFF & amp; DatePartannouncement @startDate DATETIME announcement @EndDate DATETIME announcement @TimePeriod NVARCHAR (50) Announcement @RptDate DATETIME set @startDate = '2011/01/15' Set @EndDate = ' 12/31/2011 'SET @ Time Period =' Monthly 'SET @RptDate = @startDate WHILE @RptDate & lt; @EndDate print @RptDate start --- these dates I think you want to print out if @TimePeriod = 'monthly' set to @RptDate = DATEADD (mm, 1, DATEADD (day, day's (@RptDate) + 1 , @ RptDate)) @TimePeriod = 'Weekly' set @RptDate = DATEADD (ww, 1, DATEADD (day, -DATEPART (working day, @ RptDate) + 1, @ RptDate)) @TimePeriod = 'quarterly 'Set @RptDate = DATEADD (Qq, 1, DATEADD (qq, DATEDIFF (qq, 0, rptdate), -1) + 1) end
- 50- 26 December 31 December
1 - 15 January - 16 January
-
Comments
Post a Comment