Just Testing
Testing a new post yayayay
Just a quick note here. This is an article based on the REG file from Kelly’s Korner (@www.kellys-korner-xp.com/xp_tweaks.htm). I’m not fond of downloading REG files and running them even if I have checked what it’s doing. I’m putting a note here just for me:
Do not follow the below: In the middle of doing
My Setup:
Introduction
This has to be the 3rd article on creating a calendar in SQL Server Reporting Services on this website. These should be:
Another one?
The above 2 articles relate to examples that I did using what I could from the Internet (and we all know how no one ever seems to have the same tools/environment setup as you) so they were a bit ad hoc and more of a personal opinion than anything official.
The following example comes from an actual book that my day job purchased for us. It includes a better example of an Outlook-like monthly calendar with interactivity. You could just download it from the web but I have the additional task of connecting it to an events database.
Main Calendar Generator Function
CREATE FUNCTION fn_GenerateMonth(@MonthNum int, @Year int)
RETURNS
@Month TABLE
(
-- Add the clumn definitions for the TABLE variable here
MonthDate datetime,
DayNumber int,
DayName varchar(12),
DayOfWeek int,
WeekOfMonth int,
MonthName varchar(12)
)
AS
BEGIN
DECLARE @MonthDate datetime
DECLARE @WeekOfMonth int
DECLARE @WeekFillDate datetime
DECLARE @MonthName varchar(12)
SET @WeekOfMonth = 1
-- Find the first day of the month and the month name
SET @MonthDate = RIGHT('0' + CONVERT(varchar(2), @MonthNum),2) + '/01/' + CONVERT(char(4), @Year)
SET @MonthName = DATENAME(mm, @MonthDate)
-- Back up to the first day of the week containing the first day of the month.
SET @WeekFillDate = @MonthDate
WHILE DATEPART(dw, @WeekFillDate) > 1
BEGIN
SET @WeekFillDate = DATEADD(dd, -1, @WeekFillDate)
INSERT INTO
@Month (MonthDate, DayNumber, DayName, DayOfWeek, WeekOfMonth, MonthName)
VALUES (@WeekFillDate, DAY(@WeekFillDate), DATENAME(dw, @WeekFillDate), DATEPART(dw, @WeekFillDate), @WeekOfMonth, @MonthName)
END
WHILE MONTH(@MonthDate) = @MonthNum
BEGIN
IF DATEPART(dw, @MonthDate) = 1 AND DAY(@MonthDate) > 1
BEGIN
SET @WeekOfMonth = @WeekOfMonth + 1
END
INSERT INTO
@Month (MonthDate, DayNumber, DayName, DayOfWeek, WeekOfMonth, MonthName)
VALUES (@MonthDate, DAY(@MonthDate), DATENAME(dw, @MonthDate), DATEPART(dw, @MonthDate), @WeekOfMonth, @MonthName)
SET @MonthDate = DATEADD(dd, 1, @MonthDate)
END
-- Finish up the week containing the last day fo the month.
SET @WeekFillDate = DATEADD(dd, -1, @MonthDate)
WHILE DATEPART(dw, @WeekFillDate) < 7
BEGIN
SET @WeekFillDate = DATEADD(dd, -1 , @MonthDate)
INSERT INTO
@Month (MonthDate, DayNumber, DayName, DayOfWeek, WeekOfMonth, MonthName)
VALUES (@WeekFillDate, DAY(@WeekFillDate), DATENAME(dw, @WeekFillDate), DATEPART(dw, @WeekFillDate), @WeekOfMonth, @MonthName)
END
RETURN
END
-- We can see the output from the function by running a simple test query in SQL
-- Server Management Studio, executing this command, passing 12 for the month and 2009 for the year.
-- Example: SELECT * FROM dbo.fn_GenerateMonth(12, 2009)
Do not use this article: In the middle of doing