cal 2 . . .

1

Please login or register to vote for this query.

(click on this box to dismiss)

Computational Science

Q&A for scientists using computers to solve scientific problems

declare @columnWidth tinyint = 4
declare @startDate date, @stopDate date

if ##yearsToShow:int?5## < 0 
  begin --negative value entered
    set @startDate = dateadd(year,##yearsToShow:int?5##,getdate())
    set @stopDate = getdate()
  end
else
  begin  --positive value entered
    set @startDate = getdate()
    set @stopDate = dateadd(year,##yearsToShow:int?5##,getdate())
  end
  
declare @dt date, @firstWkDay smallint, @lastWkDay smallint
declare @WkDay smallint, @WkNum smallint, @numDays smallint, @numWks smallint
declare @st nvarchar(max), @st1 nvarchar(max), @st2 nvarchar(max), @monthLabel nvarchar(max)
declare @charsToTrim_top int, @charsToTrim_btm int, @topBorder nvarchar(max)

drawMonth:
set @startdate   = format(@startdate,'yyyy-MM-01')
set @stopdate     = eomonth(@startdate)
set @numDays     = day(@stopdate)
set @firstWkDay  = datepart(weekday,@startdate)
set @lastWkDay   = datepart(weekday,@stopdate)
set @numWks    = cast((@numDays-@lastWkDay-(8-@firstWkDay))/7 as smallint)+2
set @WkNum       = 1
set @WkDay       = 1
set @dt          = dateadd(d,1-@firstWkDay,@startdate)
set @monthLabel  = format(@startdate,'MMMM yyyy')                              --reset @st: month name/year
set @charsToTrim_top = ((@firstWkDay-1)*5)+1
set @charsToTrim_btm = ((7-@lastWkDay)*5)+1

declare @Line nvarchar(30) = replicate(N'═',@columnWidth)
declare @Blank nvarchar(30) = replicate(N' ',@columnWidth)

set @topBorder   = replicate(N' ',@charsToTrim_top-1)+N'╔'+replicate(@line+N'╦',7-@firstWkDay)+@line+N'╗'+char(10)  --top borders
set @st = @topBorder


while @wkNum <= @numWks  --if there are weeks remaining in this month...
  begin                    --...then start a new day
    if @wkDay=1 --start of week
      begin
        set @st1=N'║' 
        set @st2 = case when @wkNum<@numWks then N'╠' else N'╚' end
      end
  
    set @st1 = @st1 + case when month(@dt)=month(@startdate) then format(@dt,N' dd ') else N'    ' end
    set @st2 = @st2 + @line 
    
    if @wkday=7 --end of week
      begin
      
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- tidy unused cells
        if @wknum = 1  --first week
          begin
            set @st2 = replicate(N' ',@charsToTrim_top)+substring(@st2,@charsToTrim_top+1,999)
            set @st1=replace(@st1,N'║    ',N'     ')
            set @st2=replace(@st2,N' ═',N'╬═')
            set @st2=replace(@st2,N'     ',N'╦'+@line)
            set @st2= case when @firstWkDay=1 then N'╠' else N'╔' end + substring(@st2,2,999)
          end
      
        if @wknum = @numWks-1  --second-to-last week
          begin
            set @st2 = left(@st2,len(@st2)-(@charsToTrim_btm-1))
            if @lastwkday < 7 
                   set @st2=@st2+N'╬'+replicate(@line+N'╩',7-@lastwkday-1)+@line+N'╝'
              else set @st2=@st2+N'╣'
          end
      
        if @wknum = @numWks  --last week
          begin
            set @st1=replace(@st1,N'    ║',N'     ')
            set @st2=left(@st2,len(@st2)- (@charsToTrim_btm-1))
          end
     
        set @st=@st+@st1+case when @wkday=7 and @lastwkday<7 and @wknum=@numwks then N'' else N'║' end+char(10)   
        set @st=@st+@st2+case when @wknum=@numwks-1 then N'' when @wknum=@numwks then N'╝' else N'╣' end+char(10)
        set @wknum=@wknum+1 --increment week#
      end
    else
      begin
        set @st1=@st1+N'║'
        set @st2=@st2+case when @wkNum<@numWks then N'╬' else N'╩' end
      end

--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- finished tidy; increment counters     
    set @dt = dateadd(day,1,@dt)       --finished this day, increase date
    set @wkday = datepart(weekday,@dt) --increment date
  end 
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- print month
if left(@st,len(@monthLabel))=replicate(' ',len(@monthLabel)) --optional: overlay month into 1st week if room
       set @st = @monthLabel + substring(@st, len(@monthLabel)+1,9999)  
  else set @st = @monthLabel + char(10) + @st


print @st
set @startdate = dateadd(month,1,@startdate)
if @startdate < dateadd(year,##yearsToShow:int?5##,getdate()) goto drawMonth



--yearsToShow:Enter the number of years to display
/*╔═╦╗ ╔═╦╗ ╗ ╔╗ 
  ║ ║║ ║ ╚╝ ║ ║║ 
  ╠═╬╣ ╚═╦╗ ╠═╬╣ 
  ║ ╚╝ ╚═╩╝ ╝ ╚╝ 
 ═╝              */

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results