Created
June 2, 2021 20:38
-
-
Save KartikWatts/e746c7bfc8a774c2bd0c7e0952295331 to your computer and use it in GitHub Desktop.
SQL_Queries
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
CREATE TABLE TIMEDIMENSION( | |
SKDATE varchar(20) not null, | |
Date varchar(20) not null , | |
CalendarDay int not null, | |
CalendarMonth int not null, | |
CalendarYear int not null, | |
CalendarQuarter int not null, | |
DayNameLong nvarchar(10) not null, | |
DayNameShort nvarchar(10) not null, | |
DayNumberOfWeek int not null, | |
DayNumberOfYear int not null, | |
DaySuffix nvarchar(10) not null, | |
FiscalWeek int not null, | |
FiscalPeriod int not null, | |
FiscalQuarter int not null, | |
FiscalYear int not null, | |
Period varchar(20) not null | |
); | |
--PROCEDURE STARTS HERE | |
CREATE PROCEDURE DateDimensions @StartDate Date, @EndDate Date | |
AS | |
DECLARE @sk_date varchar(20), @s_date VARCHAR(20), @calendar_day INT, @calendar_month INT, | |
@calendar_year INT, @calendar_quarter INT, @day_name_long NVARCHAR(20), @day_name_short NVARCHAR(20), | |
@day_number_of_week INT, @day_number_of_year INT, @day_suffix NVARCHAR(10), | |
@fiscal_dimension DATE, @fiscal_week INT, @fiscal_period INT, @fiscal_quarter INT, @fiscal_year INT, @fiscal_year_period VARCHAR(20); | |
IF @StartDate > @EndDate | |
BEGIN | |
SELECT 'Return Status'= 'Start Date Should be less than End Date'; | |
PRINT 'Start Date Should be less than End Date'; | |
RETURN | |
END | |
SET @sk_date= CONVERT(varchar,@StartDate,112); | |
SET @sk_date= CAST(@sk_date as varchar(20)); | |
SET @s_date= CONVERT(varchar,@StartDate,103); | |
SET @calendar_day= DAY(@StartDate); | |
SET @calendar_month= MONTH(@StartDate); | |
SET @calendar_quarter= DATEPART(q, @StartDate); | |
SET @calendar_year= YEAR(@StartDate); | |
SET @day_name_long= FORMAT(@StartDate, 'dddd'); | |
SET @day_name_short= FORMAT(@StartDate, 'ddd'); | |
--IF CONSIDERING SUNDAY AS 1 AS PER SHEET=> SET @day_number_of_week= DATEPART(dw, @StartDate); | |
SET @day_number_of_week= DATEPART(dw, @StartDate)-1; | |
SET @day_number_of_year= DATENAME(y , @StartDate); | |
SET @day_suffix= CASE | |
WHEN @calendar_day = 1 OR @calendar_day = 21 OR @calendar_day = 31 | |
THEN 'st' | |
WHEN @calendar_day = 2 OR @calendar_day = 22 | |
THEN 'nd' | |
WHEN @calendar_day = 3 OR @calendar_day = 23 | |
THEN 'rd' | |
ELSE 'th' | |
END; | |
SET @day_suffix= CONCAT(@calendar_day, @day_suffix); | |
--IF CONSIDERING FISCAL_YEAR STARTING FROM JANUARY AS PER SHEET=> SET @fiscal_dimension= @StartDate; | |
--CURRENT SOLUTION ASSUMING FISCAL YEAR STARTS ON 1st APRIL; | |
SET @fiscal_dimension= DATEADD(month, -3, @StartDate); | |
SET @fiscal_week= DATEPART(week,@fiscal_dimension); | |
SET @fiscal_period= MONTH(@fiscal_dimension); | |
SET @fiscal_quarter= DATEPART(q, @fiscal_dimension); | |
SET @fiscal_year= YEAR(@fiscal_dimension); | |
SET @fiscal_year_period= CONCAT(@fiscal_year, @fiscal_period); | |
INSERT INTO TIMEDIMENSION | |
(SKDATE, [Date], CalendarDay, CalendarMonth, CalendarYear, CalendarQuarter, | |
DayNameLong, DayNameShort, DayNumberOfWeek, DayNumberOfYear, DaySuffix, | |
FiscalWeek, FiscalPeriod, FiscalQuarter, FiscalYear, Period) | |
VALUES(@sk_date, @s_date, @calendar_day, @calendar_month, @calendar_year, @calendar_quarter, | |
@day_name_long, @day_name_short, @day_number_of_week, @day_number_of_year, @day_suffix, | |
@fiscal_week, @fiscal_period, @fiscal_quarter, @fiscal_year , @fiscal_year_period); | |
--PROCEDURE ENDS HERE | |
EXEC DateDimensions @StartDate='2003/04/29' , @EndDate='2021-05-05'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment