Created
December 14, 2016 23:27
-
-
Save marciuz/86fd689514b2e65249dbb78b31f5b557 to your computer and use it in GitHub Desktop.
Italian version. The script create a calendar table, a useful tool for many databases. It is useful and quicker then create on the fly dates.
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
-- | |
-- CALENDAR TABLE - Versione Italiana dello script descritto su | |
-- https://www.brianshowalter.com/blog/calendar_tables | |
-- Marcello Verona 2016 <marcelloverona@gmail.com> | |
-- | |
-- | |
-- Set italian as language | |
-- | |
SET lc_time_names = 'it_IT'; | |
-- | |
-- Create the table | |
-- | |
DROP TABLE IF EXISTS calendar_table; | |
CREATE TABLE calendar_table ( | |
dt DATE NOT NULL PRIMARY KEY, | |
y SMALLINT NULL, | |
q tinyint NULL, | |
m tinyint NULL, | |
d tinyint NULL, | |
dw tinyint NULL, | |
monthName VARCHAR(9) NULL, | |
dayName VARCHAR(9) NULL, | |
w tinyint NULL, | |
isWeekday BINARY(1) NULL, | |
isHoliday BINARY(1) NULL, | |
holidayDescr VARCHAR(32) NULL, | |
isPayday BINARY(1) NULL | |
) ENGINE=MyISAM DEFAULT CHARSET=utf8; | |
-- | |
-- Create work table ints | |
CREATE TABLE ints ( i tinyint ); | |
INSERT INTO ints VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); | |
-- | |
-- Populate the calendar table until the from 2010 to 2040. | |
-- SELECT datediff('2040-12-31','2010-01-01'); -> 11322 days | |
-- | |
INSERT INTO calendar_table (dt) | |
SELECT DATE('2010-01-01') + INTERVAL a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i DAY | |
FROM ints a JOIN ints b JOIN ints c JOIN ints d JOIN ints e | |
WHERE (a.i*10000 + b.i*1000 + c.i*100 + d.i*10 + e.i) <= 11322 | |
ORDER BY 1; | |
-- | |
-- Add the details | |
-- | |
UPDATE calendar_table | |
SET isWeekday = CASE WHEN dayofweek(dt) IN (1,7) THEN 0 ELSE 1 END, | |
isHoliday = 0, | |
isPayday = 0, | |
y = YEAR(dt), | |
q = quarter(dt), | |
m = MONTH(dt), | |
d = dayofmonth(dt), | |
dw = dayofweek(dt), | |
monthname = monthname(dt), | |
dayname = dayname(dt), | |
w = week(dt), | |
holidayDescr = ''; | |
-- | |
-- Start to add holidays... | |
-- | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Capodanno' WHERE m = 1 AND d = 1; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Epifania' WHERE m = 1 AND d = 6; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Liberazione' WHERE m = 4 AND d = 25; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Festa del Lavoro' WHERE m = 5 AND d = 1; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Festa della Repubblica' WHERE m = 6 AND d = 2; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Assunzione / Ferragosto' WHERE m = 8 AND d = 15; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Tutti i santi' WHERE m = 11 AND d = 1; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Immacolata' WHERE m = 12 AND d = 8; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Natale' WHERE m = 12 AND d = 25; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'Santo Stefano' WHERE m = 12 AND d = 26; | |
UPDATE calendar_table SET isHoliday = 1, holidayDescr = 'San Silvestro' WHERE m = 12 AND d = 31; | |
-- | |
-- Caso speciale: Pasqua e pasquetta. | |
-- | |
DELIMITER $$ | |
DROP FUNCTION IF EXISTS `fneastern`$$ | |
CREATE FUNCTION `fneastern`(iYear INT) RETURNS DATE | |
DETERMINISTIC | |
BEGIN | |
SET @iD=0,@iE=0,@iQ=0,@iMonth=0,@iDay=0; | |
SET @iD = 255 - 11 * (iYear % 19); | |
SET @iD = IF (@iD > 50,(@iD-21) % 30 + 21,@iD); | |
SET @iD = @iD - IF(@iD > 48, 1 ,0); | |
SET @iE = (iYear + FLOOR(iYear/4) + @iD + 1) % 7; | |
SET @iQ = @iD + 7 - @iE; | |
IF @iQ < 32 THEN | |
SET @iMonth = 3; | |
SET @iDay = @iQ; | |
ELSE | |
SET @iMonth = 4; | |
SET @iDay = @iQ - 31; | |
END IF; | |
RETURN STR_TO_DATE(CONCAT(iYear,'-',@iMonth,'-',@iDay),'%Y-%m-%d'); | |
END$$ | |
DELIMITER ; | |
UPDATE calendar_table c1 SET isHoliday = 1, holidayDescr = 'Pasqua' WHERE dt=fneastern(y); | |
UPDATE calendar_table c1 SET isHoliday = 1, holidayDescr = 'Lunedì di Pasquetta' WHERE dt=DATE_ADD(fneastern(y), INTERVAL 1 DAY); | |
-- Delete the function | |
DROP FUNCTION IF EXISTS fneastern; | |
-- Delete the ints table | |
DROP TABLE ints; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment