Important alert: (current site time 7/15/2013 8:04:58 PM EDT)
 

VB icon

SQL CALENDAR

Email
Submitted on: 9/20/2002 3:01:38 PM
By: John Overton 
Level: Advanced
User Rating: By 11 Users
Compatibility: SQL Server 2000, SQL Server 7.0, SQL Server 6.5 and earlier
Views: 34503
 
     UPDATED:Produces Full Calendar for each Month in a Year. Just pass year as an integer. Let me know if it proves useful. Call it Like: Execute Calendar 2002
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: SQL CALENDAR
-- Description:UPDATED:Produces Full Calendar for each Month in a Year. Just pass year as an integer. Let me know if it proves useful. Call it Like: 
Execute Calendar 2002
-- By: John Overton
--
-- Inputs:@year(int)
--
-- Returns:Calendar for each month
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=546&lngWId=5--for details.--**************************************

--**************************************
-- 
-- Name: SQL CALENDAR
-- Description:Produces Full Calendar fo
-- r each Month in a Year. Just pass year a
-- s an integer. Let me know if it proves u
-- seful.
-- By: John Overton
--
-- Inputs:@year(int)
--
-- Returns:Calendar for each month
--
--This code is copyrighted and has-- limited warranties.Please see http://
-- www.Planet-Source-Code.com/xq/ASP/txtCod
-- eId.546/lngWId.5/qx/vb/scripts/ShowCode.
-- htm--for details.--**************************************
-- 
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
create procedure CALENDAR (@YEAR INT) AS
DECLARE @INPUTDATE DATETIME
DECLARE @DATE DATETIME
DECLARE @LASTDATE DATETIME
DECLARE @MONTHDAYCOUNT INT
DECLARE @COUNT INT
DECLARE @DAY VARCHAR(10)
DECLARE @STARTWEEK INT
DECLARE @CURWEEK INT
DECLARE @STARTMONTH INT
SET @INPUTDATE='01/01/' + CAST(@YEAR AS CHAR(4))
PRINT @INPUTDATE
SET @STARTMONTH=1
WHILE @STARTMONTH<=12
BEGIN
SET @COUNT=1
SET @DATE = DATEADD(d, -(DATEPART(dd, @INPUTDATE) - 1), @INPUTDATE)
SET @LASTDATE=DATEADD(DD,-1,DATEADD(MM,1,@DATE))
SET @MONTHDAYCOUNT=datediff(d, @date, dateadd(m, 1, @date))
SET @STARTWEEK=DATENAME(WEEK,@DATE)
DECLARE @CURRWEEK INT
DECLARE @CUR INT
CREATE TABLE #TEMP(
	WEEK VARCHAR(10),
	SUNDAY VARCHAR(10),
	MONDAY VARCHAR(10),
	TUESDAY VARCHAR(10),
	WEDNESDAY VARCHAR(10),
	THURSDAY VARCHAR(10),
	FRIDAY VARCHAR(10),
	SATURDAY VARCHAR(10))
DECLARE @wkcount int
DECLARE @weeksinmonth int
DECLARE @EXEC NVARCHAR(2000)
SET @WKCOUNT=1
SET @weeksinmonth=datediff(week, @date, @lastdate) + 1
WHILE @wkcount<= @weeksinmonth
begin
INSERT INTO #TEMP VALUES(@wkcount,'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY')
SET @WKCOUNT=@WKCOUNT + 1
end
WHILE @COUNT<=@MONTHDAYCOUNT
BEGIN
	SET @DAY=DATENAME(WEEKDAY,@DATE)
	IF @STARTWEEK=DATENAME(WEEK,@DATE)
		SET @CURRWEEK=1
	ELSE
	BEGIN
		SET @CUR=DATENAME(WEEK,@DATE)
		SET @CURRWEEK=(@CUR-@STARTWEEK)+1
	
		
	END
	
	SET @EXEC='UPDATE #TEMP SET ' + @DAY + ' =' + CAST(@COUNT AS CHAR(2)) + ' WHERE WEEK=' + CAST(@CURRWEEK AS CHAR(2))+ 'AND WEEK IS NOT NULL'
	EXEC SP_EXECUTESQL @EXEC
	SET @DATE=DATEADD(DD,1,@DATE)
	SET @COUNT=@COUNT + 1
END
UPDATE #TEMP SET SUNDAY=' ' WHERE SUNDAY='SUNDAY'
UPDATE #TEMP SET MONDAY=' ' WHERE MONDAY='MONDAY'
UPDATE #TEMP SET TUESDAY=' ' WHERE TUESDAY='TUESDAY'
UPDATE #TEMP SET WEDNESDAY=' ' WHERE WEDNESDAY='WEDNESDAY'
UPDATE #TEMP SET THURSDAY=' ' WHERE THURSDAY='THURSDAY'
UPDATE #TEMP SET FRIDAY=' ' WHERE FRIDAY='FRIDAY'
UPDATE #TEMP SET SATURDAY=' ' WHERE SATURDAY='SATURDAY'
SELECT SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY FROM #TEMP
DROP TABLE #TEMP
SET @INPUTDATE=DATEADD(MM,1,@INPUTDATE)
SET @STARTMONTH=@STARTMONTH+1
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
9/21/2002 11:49:50 AMMarkus Q

Procedure only returns a single month (January). You may want to have it take two parameters, a month and year. Good work though.
(If this comment was disrespectful, please report it.)

 
9/22/2002 4:16:12 AM

I would like to get a code that can be used to grade the students marks in VB 6.0 and also how to inter-Link diffrent forms
(If this comment was disrespectful, please report it.)

 
9/22/2002 12:41:11 PM

This was great!!! In answer to MarkusQ's statment it does giv you all months, but you will have to scroll deep for the results. to set for use with month, get rid of the loop, and add the month to the input and to the string that builds the input date.

Worked Great for me!
(If this comment was disrespectful, please report it.)

 
10/31/2002 10:10:30 AMJohn Overton

Thanks for the votes!
(If this comment was disrespectful, please report it.)

 
12/16/2002 9:39:24 AM

Very, very nice.

I have made a couple of changes for you to look at:

1)
CREATE PROCEDURE CALENDAR (@YEAR INT) AS
set nocount on -- ADD THIS LINE AFTER THE ONE ABOVE

2)
PRINT " " + upper(left(cast(@INPUTDATE as varchar),3)) --ADD THIS LINE ABOVE THE BELOW LINE
SELECT SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY FROM #TEMP

If you make these two changes, you will see the month centered above each 'calendar' and none of the 'rows affected' will show. Just the Month and Calendar.
(If this comment was disrespectful, please report it.)

 
12/16/2002 9:42:31 AM

Please note that the number of spaces between the "s in the above suggested changes in number 2 is 36.

Have fun, Phred
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.