Print The Calender
Friends, i am not so much exerpience person in sql. But i am just trying to do my best.
Here, i have create a store procedure that shows you complete year's calender. To show the complete year calender, you just run the script and execute the store procedure.
Leave your comments, and tell me how can i impore my sql skills.
Thanks for reading.
Vinay K
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[calender]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[calender]
GO
/***************************************************************************************
Created By :- Vinay Kumar
Created on :- 29 Jan 2008
Purpose :- Print the calender from the given Date
****************************************************************************************/
Create Procedure calender
@StartDate DateTime='01/01/2008' ----Default Value
As
Set nocount on
Declare @tbl Table(RowID int identity(1,1), Mon varchar(20) default '', Tue varchar(20) default '', Wed varchar(20) default '',
Thu varchar(20) default '', Fri varchar(20) default '', Sat varchar(20) default '', Sun varchar(20) default '')
declare @currentMonth int
declare @CurrentDate int
declare @CurrentDay int
declare @CurrentRowID int
declare @TotalMonthDays int
declare @monthName varchar(20)
declare @YearNo int
set @YearNo = datepart(yy, @StartDate)
set @CurrentDate = datepart(dd, @StartDate)
set @currentMonth = datepart(mm, @StartDate)
set @CurrentDay = datepart(dw, @StartDate) - 2
insert @tbl(Mon) values ('')
set @CurrentRowID = @@Identity
while @currentMonth <= 12
begin
set @monthName = datename(m, dateadd(m, @currentMonth - 1, getdate()))
insert @tbl(Wed, thu, Fri) values (@monthName, ' - ', Convert(Varchar, @YearNo))
set @CurrentRowID = @@Identity
insert @tbl(mon,tue,Wed, thu, Fri,sat,sun) values ('________', '________', '________', '________', '________', '________', '________')
insert @tbl(Mon) values ('')
set @CurrentRowID = @@Identity
set @TotalMonthDays = case when @currentMonth in (1,3,5,7,8,10,12) then 31
when @currentMonth in (4,6,9,11) then 30
when @currentMonth = 2 and @YearNo % 4 = 0 then 29
when @currentMonth = 2 and @YearNo % 4 <> 0 then 28
end
while @CurrentDate <= @TotalMonthDays
begin
if @CurrentDay = 0
begin
insert @tbl(Mon) values ('')
set @CurrentRowID = @@Identity
end
if @CurrentDay = 0
update @tbl set Mon = Convert(Varchar(2), @CurrentDate) where RowID = @CurrentRowID
else if @CurrentDay = 1
update @tbl set Tue = Convert(Varchar(2), @CurrentDate) where RowID = @CurrentRowID
else if @CurrentDay = 2
update @tbl set Wed = Convert(Varchar(2), @CurrentDate) where RowID = @CurrentRowID
else if @CurrentDay = 3
update @tbl set Thu = Convert(Varchar(2), @CurrentDate) where RowID = @CurrentRowID
else if @CurrentDay = 4
update @tbl set Fri = Convert(Varchar(2), @CurrentDate) where RowID = @CurrentRowID
else if @CurrentDay = 5
update @tbl set Sat = Convert(Varchar(2), @CurrentDate) where RowID = @CurrentRowID
else if @CurrentDay = 6
update @tbl set Sun = Convert(Varchar(2), @CurrentDate) where RowID = @CurrentRowID
set @CurrentDate = @CurrentDate + 1
if @CurrentDate = @TotalMonthDays - 1
Begin
set @currentMonth = @currentMonth + 1
end
set @CurrentDay = (@CurrentDay + 1) % 7
end
set @CurrentDate = 1
end
-- Finaly show the output
select Mon, Tue, Wed, Thu, Fri, Sat, Sun from @tbl where Mon <> '' or Tue <> '' or Wed <> '' or
Thu <> '' or Fri <> '' or Sat <> '' or Sun <> ''