• 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