Created
July 1, 2015 08:03
-
-
Save ststeiger/d2bc86a8d45d6169a33b to your computer and use it in GitHub Desktop.
List quarters from/to in year-range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DECLARE @in_yearFrom int | |
| DECLARE @in_yearTo int | |
| DECLARE @in_QuartalFrom int | |
| DECLARE @in_QuartalTo int | |
| SET @in_yearFrom = 1800 | |
| SET @in_yearTo = 1805 | |
| SET @in_QuartalFrom = 2 | |
| SET @in_QuartalTo = 3 | |
| ;WITH CTE AS | |
| ( | |
| SELECT | |
| 1 AS q | |
| ,@in_yearFrom AS y | |
| ,DATEADD(year, @in_yearFrom - 1900, CAST(0 AS datetime)) AS s | |
| ,DATEADD(month, 3, DATEADD(year, @in_yearFrom - 1900, CAST(0 AS datetime))) -1 AS e | |
| UNION ALL | |
| SELECT | |
| (CTE.q%4) + 1 AS q | |
| ,YEAR(DATEADD(month, 3, CTE.s)) AS y | |
| ,DATEADD(month, 3, CTE.s) AS s | |
| ,DATEADD(month, 3, DATEADD(month, 3, CTE.s)) -1 AS e | |
| FROM CTE | |
| WHERE DATEADD(month, 3, CTE.s) < DATEADD(year, 1 + @in_yearTo - 1900, CAST(0 AS datetime)) | |
| ) | |
| SELECT * FROM CTE | |
| WHERE (1=1) | |
| AND s >= DATEADD(month, (@in_QuartalFrom-1) * 3, DATEADD(year, @in_yearFrom - 1900, CAST(0 AS datetime))) | |
| AND s <= DATEADD(month, (@in_QuartalTo-1) * 3, DATEADD(year, @in_yearTo - 1900, CAST(0 AS datetime))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment