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

VB icon

SQL Calendar (Updated)

Email
Submitted on: 3/1/2003 10:06:29 AM
By: John Overton 
Level: Advanced
User Rating: By 8 Users
Compatibility: SQL Server 2000
Views: 17695
 
     Displays complete calendar of any given year. Updated version of original code... Easy to alter to fit your needs. Call--- Execute Calendar 2003
 
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 (Updated)
-- Description:Displays complete calendar of any given year. Updated version of original code... Easy to alter to fit your needs.
Call--- Execute Calendar 2003
-- By: John Overton
--
-- Inputs:Year-- example: 2003
--
-- Returns:Calendar
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=640&lngWId=5--for details.--**************************************

-- 
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
alter PROCEDURE CALENDAR (@YEAR INT) AS
set nocount on
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'
set @exec='
 SELECT "' + CAST(@YEAR AS CHAR(4)) + '" as "' + datename(month,@inputdate) + '",SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY FROM #TEMP'
exec sp_executesql @exec 
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
execute CALENDAR 2003


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
3/3/2003 2:21:43 AMmiracl

very cool code !, 5 globes
(If this comment was disrespectful, please report it.)

 
11/26/2003 11:32:34 AM01010101

Is there a way to specify one month of that year to just get that month/year combination?
(If this comment was disrespectful, please report it.)

 
5/29/2006 5:01:16 AMimran

Very Cool Very Nice Procedure 5 globes from me too
(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.