Feeds:
Posts
Comments

Posts Tagged ‘sql date’


Declare @FromDate Date = '2009/01/01',
		@ToDate Date = '2012/02/15'

	;WITH cteDate as
	(
		SELECT 
			DATEADD(QQ,DATEDIFF(QQ,0,@ToDate),0) MaxDate
	)
	, cteDerived as 
	(	SELECT
			1 AS RID, 
			DATEADD(QQ,DATEDIFF(QQ,0,@FromDate),0) MinDate
		UNION ALL 
		SELECT
			RID + 1,
			DATEADD(QQ,1,MinDate)
		FROM
			cteDerived
		WHERE
			DATEADD(QQ,1,MinDate) <= (Select MaxDate From cteDate)
	)
	Select 
		'Q' + CAST(RID as Varchar(3)) as [Quarter], 
		MinDate as FromDate, 
		DATEADD(QQ,1,MinDate)-1 as ToDate 
	FROM 
		cteDerived
	OPTION (MAXRECURSION 0) 

Read Full Post »