Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Print Text Month Calendar


Print Text Month Calendar

Author
Message
diaz.bernabe
diaz.bernabe
Old Hand
Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)

Group: General Forum Members
Points: 313 Visits: 194
Comments posted to this topic are about the item Print Text Month Calendar
Pete Cox
Pete Cox
SSC-Enthusiastic
SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)

Group: General Forum Members
Points: 106 Visits: 277
Hi Bernabe,
Nice idea if you have the need.

I was thinking of a less procedural approach and came up with this.

Given a "Calendar Table" with the following definition

CREATE TABLE [dbo].[Dates](
[CalendarDate] [smalldatetime] NOT NULL,
[isWeekday] [bit] NULL,
[isHoliday] [bit] NULL,
[CalendarYear] [smallint] NULL,
[CalendarQuarter] [tinyint] NULL,
[CalendarMonth] [tinyint] NULL,
[FinancialYear] [smallint] NULL,
[FinancialQuarter] [tinyint] NULL,
[FinancialMonth] [tinyint] NULL,
[DayOfMonth] [tinyint] NULL,
[DayOfWeek] [tinyint] NULL,
[MonthName] [varchar](9) NULL,
[ShortMonthName] [char](3) NULL,
[DayName] [varchar](9) NULL,
[ShortDayName] [char](3) NULL,
[SQL_Week] [tinyint] NULL,
[Week] [tinyint] NULL,
[HolidayDescription] [varchar](255) NULL
) ON [PRIMARY]

the following T-SQL seems to work quite well

DECLARE @supplieddate DATETIME
DECLARE @lastdayofmonth DATETIME
DECLARE @firstdayofmonth DATETIME
DECLARE @dayofweek INT
DECLARE @calendarbody VARCHAR(MAX)
DECLARE @title VARCHAR(MAX)
DECLARE @monthname VARCHAR(12)
DECLARE @paddedmonthname VARCHAR(24)
DECLARE @newline CHAR

SET @newline = CHAR(13)
SET @supplieddate = DATEADD(MONTH,-0,CURRENT_TIMESTAMP)

SELECT
@dayofweek = DATEPART(dw,MIN(CalendarDate)),
@firstdayofmonth = MIN(CalendarDate),
@lastdayofmonth = MAX(CalendarDate),
@monthname=DATENAME(MONTH,@supplieddate)
FROM Dates
WHERE CalendarMonth = MONTH(@supplieddate) AND CalendarYear = YEAR(@supplieddate)
GROUP BY CalendarMonth, CalendarYear


SET @paddedmonthname = REPLICATE(' ',6) +
@monthname +
' ' +
RTRIM(CAST(YEAR(@supplieddate) AS CHAR)) +
REPLICATE(' ',6 + (LEN(@monthname) % 2))

SET @title = ' <<' +
SUBSTRING(@paddedmonthname, (LEN(@paddedmonthname) / 2) - 4 + (LEN(@paddedmonthname) % 2), 16) +
'>>'
+@newline
+' Su Mo Tu We Th Fr Sa';

SET @calendarbody = REPLICATE(' ', 3 * (@dayofweek - 1))

SELECT
@calendarbody = @calendarbody +
RIGHT(' ' + CAST(DayofMonth AS VARCHAR(5)),3) +
(CASE WHEN DayOfWeek = 7 THEN @newline ELSE '' END)
FROM Dates
WHERE CalendarMonth = MONTH(@supplieddate) AND CalendarYear = YEAR(@supplieddate)

PRINT @title + @newline + @calendarbody


I realise that I am using the quirky update feature, but thought it was OK for now

My apologies for the poor formatting,
Pete
Bill Talada
Bill Talada
Old Hand
Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)Old Hand (381 reputation)

Group: General Forum Members
Points: 381 Visits: 1810
Here is a maintainable version...

DECLARE
@dt datetime,
@DayOneDayOfWeek int,
@DaysInMonth int,
@s varchar(200),
@crlf varchar(2)
;

SET @dt = '20130401';
SET @crlf = char(13) + char(10);

-- get max days in month: calculate first day of the next month and subtract one day
SET @DaysInMonth = (select datepart(day, dateadd(day, -1, dateadd(month, 1, cast(convert(varchar(8), @dt, 121)+'01' as datetime)))));

-- need to know day one weekday
SET @DayOneDayOfWeek = (select DatePart(WeekDay, cast(convert(varchar(8), @dt, 121)+'01' as datetime)));

-- build day numbers string and pad for first day
SET @s = REPLACE(SUBSTRING(' << January YEAR >> << February YEAR >> << March YEAR >> << April YEAR >> << May YEAR >> << June YEAR >> << July YEAR >> << August YEAR >> << September YEAR >> << October YEAR >> << November YEAR >> << December YEAR >>', (DatePart(month, @dt) - 1) * 21 + 1, 21), 'YEAR', cast(DatePart(year, @dt) as varchar(4)))
+ ' Su Mo Tu We Th Fr Sa'
+ replicate(' ', @DayOneDayOfWeek - 1)
+ left(' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31', @DaysInMonth * 3)
;

-- Format for display by removing leading space and adding CRLF for each line.
print
SUBSTRING(@s, 2, 20)
+ @crlf + SUBSTRING(@s, 23, 20)
+ @crlf + SUBSTRING(@s, 44, 20)
+ @crlf + SUBSTRING(@s, 65, 20)
+ @crlf + SUBSTRING(@s, 86, 20)
+ @crlf + SUBSTRING(@s, 107, 20)
+ @crlf + SUBSTRING(@s, 128, 20)
+ case when len(@s) > 149 then @crlf + SUBSTRING(@s, 149, 20) else '' end
;



diaz.bernabe
diaz.bernabe
Old Hand
Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)

Group: General Forum Members
Points: 313 Visits: 194
Bill, I like your version!

Thanks.
Iwas Bornready
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8664 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search