DECLARE @DateFrom DATETIME,@DateTo DATETIME
DECLARE @Dates NVARCHAR(2000), @Buckets NVARCHAR(2000)
set @Dates ='10-Mar-2015,15-Mar-2015,25-Apr-2015'
set @Buckets ='0-30,31-60,61-90'
Declare @Start_Bucket int, @End_Bucket int
select TOP 1 @DateFrom= IDField from dbo.FN_SPLIT(@Dates,',')
Order By CAST(IDField aS DATETIME)
select TOP 1 @DateTo= IDField from dbo.FN_SPLIT(@Dates,',')
Order By CAST(IDField aS DATETIME) desc
if (DATEPART(day, @DateTo)=25)
BEGIN
SELECT @DateTo=DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@DateTo)+1,0))
END
Set @Buckets=REPLACE(@Buckets,'''','')
SELECT @Start_Bucket=
SUBSTRING(SUBSTRING(@Buckets,0 ,CHARINDEX(',', @Buckets)),0,
CHARINDEX('-', SUBSTRING(@Buckets,0 ,CHARINDEX(',', @Buckets))))
if CHARINDEX('-',(reverse(left(reverse(@Buckets), charindex(',', reverse(@Buckets)) -1)) ))!=0
SELECT @End_Bucket=
SUBSTRING(reverse(left(reverse(@Buckets), charindex(',', reverse(@Buckets)) -1)),
CHARINDEX('-',reverse(left(reverse(@Buckets), charindex(',', reverse(@Buckets)) -1)))+1,
LEN(reverse(left(reverse(@Buckets), charindex(',', reverse(@Buckets)) -1))) )
PRINT @DateFrom
PRINT @DateTo
PRINT @Start_Bucket
PRINT @End_Bucket