Last active
August 29, 2015 14:19
-
-
Save dataneek/e7513f7ae4bf7b5efb11 to your computer and use it in GitHub Desktop.
The following snippet generates a date-based dataset using the parameters as boundaries.
This file contains 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
public class Period | |
{ | |
public int PeriodID { get; set; } | |
public DateTime DateFrom { get; set; } | |
public DateTime DateTo { get; set; } | |
public int YearValue { get; set; } | |
public int MonthValue { get; set; } | |
public string MonthName { get; set; } | |
public int DayOfMonth { get; set; } | |
public int DayOfYear { get; set; } | |
public int WeekOfYear { get; set; } | |
public string WeekdayName { get; set; } | |
} |
This file contains 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 @DateLowerbound AS DATETIME | |
DECLARE @DateUpperbound AS DATETIME | |
SET @DateLowerbound = '01/01/1800' | |
SET @DateUpperbound = '12/31/2500' | |
; | |
WITH tResult( DateInstance ) AS | |
( | |
SELECT @DateLowerbound | |
UNION ALL | |
SELECT | |
DATEADD(day, 1, DateInstance ) AS Period | |
FROM | |
tResult | |
WHERE | |
DateInstance < @DateUpperbound | |
) | |
SELECT | |
REPLACE( CONVERT( VARCHAR(10), DateInstance, 111 ), '/', '' ) AS PeriodID | |
, DateInstance AS DateFrom | |
, DATEADD( ss, -1, DATEADD( day, 1, DateInstance ) ) AS DateTo | |
, DATENAME( yy, DateInstance ) AS YearValue | |
, MONTH( DateInstance ) AS MonthValue | |
, DATENAME( mm, DateInstance ) AS MonthName | |
, DAY( DateInstance ) AS DayOfMonth | |
, DATENAME( dy, DateInstance ) as DayOfYear | |
, DATENAME( wk, DateInstance ) as WeekOfYear | |
, DATENAME( dw, DateInstance ) as WeekdayName | |
FROM | |
tResult | |
OPTION(MAXRECURSION 0) |
This file contains 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
CREATE TABLE [dbo].[Period] | |
( | |
[PeriodID] [int] NOT NULL, | |
[DateFrom] [datetime] NOT NULL, | |
[DateTo] [datetime] NOT NULL, | |
[YearValue] [int] NOT NULL, | |
[MonthName] [nvarchar](30) NOT NULL, | |
[MonthValue] [int] NOT NULL, | |
[DayOfMonth] [int] NOT NULL, | |
[DayOfYear] [int] NOT NULL, | |
[WeekOfYear] [int] NOT NULL, | |
[WeekdayName] [nvarchar](30) NOT NULL, | |
[IsArchived] [bit] NOT NULL CONSTRAINT [DF_Period_IsArchived] DEFAULT ((0)), | |
[Created] [datetimeoffset](7) NOT NULL CONSTRAINT [DF_Period_Created] DEFAULT (sysdatetimeoffset()), | |
[LastUpdated] [datetimeoffset](7) NULL, | |
[TImestamp] [timestamp] NOT NULL, | |
CONSTRAINT [PK_Period] PRIMARY KEY CLUSTERED | |
( | |
[PeriodID] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) ON [PRIMARY] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment