<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; UDF to Return a Calendar for Any Date for Any Year</title>
	<atom:link href="http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 10 Feb 2012 02:12:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: ahmed</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-186314</link>
		<dc:creator><![CDATA[ahmed]]></dc:creator>
		<pubDate>Tue, 01 Nov 2011 08:37:48 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-186314</guid>
		<description><![CDATA[yes the server language is not english]]></description>
		<content:encoded><![CDATA[<p>yes the server language is not english</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-182924</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Mon, 24 Oct 2011 14:17:02 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-182924</guid>
		<description><![CDATA[It means that the default language of the server is not ENGLISH. Run this to know the server&#039;s language

select @@language]]></description>
		<content:encoded><![CDATA[<p>It means that the default language of the server is not ENGLISH. Run this to know the server&#8217;s language</p>
<p>select @@language</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ahmed</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-182359</link>
		<dc:creator><![CDATA[ahmed]]></dc:creator>
		<pubDate>Sun, 23 Oct 2011 13:40:55 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-182359</guid>
		<description><![CDATA[thanks a lot
how can i solve this problem on sql server 2005
datename(mm, getdate()) on management studio on server is  october
datename(mm, getdate()) on remote sql server 2005 is hijri date like shwaal
can you help me please]]></description>
		<content:encoded><![CDATA[<p>thanks a lot<br />
how can i solve this problem on sql server 2005<br />
datename(mm, getdate()) on management studio on server is  october<br />
datename(mm, getdate()) on remote sql server 2005 is hijri date like shwaal<br />
can you help me please</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: naveen7</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-115373</link>
		<dc:creator><![CDATA[naveen7]]></dc:creator>
		<pubDate>Mon, 31 Jan 2011 13:42:50 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-115373</guid>
		<description><![CDATA[Hi 

When i gave  the input date as   &#039;2011/01/30&#039;  or  &#039;31 Jan 2011&#039;   in the result set 1st Jan 2001 is not displaying.]]></description>
		<content:encoded><![CDATA[<p>Hi </p>
<p>When i gave  the input date as   &#8217;2011/01/30&#8242;  or  &#8217;31 Jan 2011&#8242;   in the result set 1st Jan 2001 is not displaying.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neelakandan</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-85096</link>
		<dc:creator><![CDATA[Neelakandan]]></dc:creator>
		<pubDate>Tue, 24 Aug 2010 09:34:35 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-85096</guid>
		<description><![CDATA[GREAT WORK]]></description>
		<content:encoded><![CDATA[<p>GREAT WORK</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nirav</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-65012</link>
		<dc:creator><![CDATA[Nirav]]></dc:creator>
		<pubDate>Sun, 11 Apr 2010 20:29:58 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-65012</guid>
		<description><![CDATA[Hi, 

i am looking for Function which return previous 6 week Monday Date from current date .

let me know.]]></description>
		<content:encoded><![CDATA[<p>Hi, </p>
<p>i am looking for Function which return previous 6 week Monday Date from current date .</p>
<p>let me know.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gayatri</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-58701</link>
		<dc:creator><![CDATA[gayatri]]></dc:creator>
		<pubDate>Wed, 16 Dec 2009 06:43:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-58701</guid>
		<description><![CDATA[Hi,

  could you please let me know how can we  retrieve the same within date ranges.I mean I need to get the same way within a date range that user selects.I need some thing like same output as above and based on each date of weekday  i need to calculate some percentages by giving the date as input.Please help me.

Thanks in Advance]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>  could you please let me know how can we  retrieve the same within date ranges.I mean I need to get the same way within a date range that user selects.I need some thing like same output as above and based on each date of weekday  i need to calculate some percentages by giving the date as input.Please help me.</p>
<p>Thanks in Advance</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Moden</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-53219</link>
		<dc:creator><![CDATA[Jeff Moden]]></dc:creator>
		<pubDate>Wed, 24 Jun 2009 06:04:47 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-53219</guid>
		<description><![CDATA[Phil,

I can understand an app needing a calendar... but why not use the built in calendar tools of the language you&#039;re writing the app in?]]></description>
		<content:encoded><![CDATA[<p>Phil,</p>
<p>I can understand an app needing a calendar&#8230; but why not use the built in calendar tools of the language you&#8217;re writing the app in?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Moden</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-53218</link>
		<dc:creator><![CDATA[Jeff Moden]]></dc:creator>
		<pubDate>Wed, 24 Jun 2009 06:02:50 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-53218</guid>
		<description><![CDATA[Phil Factor,

I guess I don&#039;t really understand why one would need to use T-SQL to build a calendar for a GUI that should have a built in feature that does that as part of &quot;the language&quot; already.]]></description>
		<content:encoded><![CDATA[<p>Phil Factor,</p>
<p>I guess I don&#8217;t really understand why one would need to use T-SQL to build a calendar for a GUI that should have a built in feature that does that as part of &#8220;the language&#8221; already.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ADP</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-45545</link>
		<dc:creator><![CDATA[ADP]]></dc:creator>
		<pubDate>Mon, 12 Jan 2009 09:09:06 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-45545</guid>
		<description><![CDATA[CREATE FUNCTION [dbo].[ufn_get_month] (@q DATETIME)  
RETURNS @table TABLE(	ID_week 	INT, 
			Sunday 		DATETIME, 
			Monday 	DATETIME, 
			Tuesday 	DATETIME, 
			Wednesday 	DATETIME, 
			Thursday 	DATETIME, 
			Friday 		DATETIME,
			Saturday 	DATETIME)
/*--------------------------------------------------------------------------------------------------------
This query returns all the days of the month containing the input date
in a standard S,M,T,W,T,F,S type table.
--------------------------------------------------------------------------------------------------------*/
AS

BEGIN

--------------------------------------------------------------------------------------------------------
--Get 1st day of month.
--------------------------------------------------------------------------------------------------------
DECLARE    	@y 	VARCHAR(4), 
		@m 	VARCHAR(2)
SELECT          	@y 	= CAST(YEAR(@q) 	AS VARCHAR),
                        	@m	= CAST(MONTH(@q) 	AS VARCHAR)
SELECT        	@q 	= CAST(@y+&#039;/&#039;+@m+&#039;/1&#039; 	AS DATETIME)

--------------------------------------------------------------------------------------------------------
--Declare temp table
--------------------------------------------------------------------------------------------------------
DECLARE @t 		TABLE(	ID          	INT IDENTITY(1,1),
                                        	ID_week	INT,
                                                	ID_datetime     	DATETIME,
                                                	ID_datename   	VARCHAR(20))

DECLARE 	@insertDate DATETIME
SELECT            	@insertDate = @q

--------------------------------------------------------------------------------------------------------
--Get all the days of the month
--uses a while loop to get all the days, 
--if user has a &#039;numbers&#039; table, this would be preferred option.
--as using a loop isn&#039;t strictly a set-based query type,
--its more of a cursor type which should be avoided.
--Cursors generally suck but sometimes the best option.
--------------------------------------------------------------------------------------------------------
WHILE MONTH(@insertDate) = MONTH(@q)
BEGIN
        INSERT INTO         @t	(ID_datetime,
				ID_week, 
				ID_datename)
        VALUES                   (@insertDate, 
			DATEPART(ww,@insertDate),
			DATENAME(dw,@insertDate))
	--incrament the insertion date and continue...
         SELECT @insertDate = cast((cast(@insertDate AS FLOAT) + 1) AS DATETIME)
END


--------------------------------------------------------------------------------------------------------
--Select the values using left outer joins on the week number 
--and the day number...
--------------------------------------------------------------------------------------------------------
INSERT INTO @table(	ID_week, 
			Sunday, 
			Monday, 
			Tuesday, 
			Wednesday, 
			Thursday, 
			Friday, 
			Saturday)

SELECT	Ref.ID_week	AS ID_week, 
                	su.ID_datetime 	AS Sunday, 
                	m.ID_datetime  	AS Monday,
                	tu.ID_datetime 	AS Tuesday,
                	we.ID_datetime 	AS Wednesday,
                	th.ID_datetime 	AS Thurday,
                	fr.ID_datetime 	AS Friday,
		sa.ID_datetime	AS Saturday
FROM
(SELECT DISTINCT 	ID_week	
FROM 			@t) 		AS Ref
LEFT OUTER JOIN @t 			AS su 	
ON 	su.ID_week 			= Ref.ID_week 	
AND 	DATEPART(dw,su.ID_datetime) 	= 1
LEFT OUTER JOIN @t 			AS m 	
ON 	m.ID_week 			= Ref.ID_week 	
AND 	DATEPART(dw,m.ID_datetime) 	= 2
LEFT OUTER JOIN @t 			AS tu 	
ON 	tu.ID_week			 = Ref.ID_week 	
AND 	DATEPART(dw,tu.ID_datetime) 	= 3
LEFT OUTER JOIN @t 			AS we 	
ON 	we.ID_week 			= Ref.ID_week 	
AND 	DATEPART(dw,we.ID_datetime) 	= 4
LEFT OUTER JOIN @t 			AS th 	
ON 	th.ID_week 			= Ref.ID_week 	
AND 	DATEPART(dw,th.ID_datetime) 	= 5
LEFT OUTER JOIN @t 			AS fr 	
ON 	fr.ID_week 			= Ref.ID_week 	
AND 	DATEPART(dw,fr.ID_datetime) 	= 6
LEFT OUTER JOIN @t 			AS sa 	
ON 	sa.ID_week 			= Ref.ID_week 	
AND 	DATEPART(dw,sa.ID_datetime) 	= 7

--------------------------------------------------------------------------------------------------------
--Return the table
--------------------------------------------------------------------------------------------------------
RETURN

END]]></description>
		<content:encoded><![CDATA[<p>CREATE FUNCTION [dbo].[ufn_get_month] (@q DATETIME)<br />
RETURNS @table TABLE(	ID_week 	INT,<br />
			Sunday 		DATETIME,<br />
			Monday 	DATETIME,<br />
			Tuesday 	DATETIME,<br />
			Wednesday 	DATETIME,<br />
			Thursday 	DATETIME,<br />
			Friday 		DATETIME,<br />
			Saturday 	DATETIME)<br />
/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
This query returns all the days of the month containing the input date<br />
in a standard S,M,T,W,T,F,S type table.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;*/<br />
AS</p>
<p>BEGIN</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8211;Get 1st day of month.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
DECLARE    	@y 	VARCHAR(4),<br />
		@m 	VARCHAR(2)<br />
SELECT          	@y 	= CAST(YEAR(@q) 	AS VARCHAR),<br />
                        	@m	= CAST(MONTH(@q) 	AS VARCHAR)<br />
SELECT        	@q 	= CAST(@y+&#8217;/'+@m+&#8217;/1&#8242; 	AS DATETIME)</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8211;Declare temp table<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
DECLARE @t 		TABLE(	ID          	INT IDENTITY(1,1),<br />
                                        	ID_week	INT,<br />
                                                	ID_datetime     	DATETIME,<br />
                                                	ID_datename   	VARCHAR(20))</p>
<p>DECLARE 	@insertDate DATETIME<br />
SELECT            	@insertDate = @q</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8211;Get all the days of the month<br />
&#8211;uses a while loop to get all the days,<br />
&#8211;if user has a &#8216;numbers&#8217; table, this would be preferred option.<br />
&#8211;as using a loop isn&#8217;t strictly a set-based query type,<br />
&#8211;its more of a cursor type which should be avoided.<br />
&#8211;Cursors generally suck but sometimes the best option.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
WHILE MONTH(@insertDate) = MONTH(@q)<br />
BEGIN<br />
        INSERT INTO         @t	(ID_datetime,<br />
				ID_week,<br />
				ID_datename)<br />
        VALUES                   (@insertDate,<br />
			DATEPART(ww,@insertDate),<br />
			DATENAME(dw,@insertDate))<br />
	&#8211;incrament the insertion date and continue&#8230;<br />
         SELECT @insertDate = cast((cast(@insertDate AS FLOAT) + 1) AS DATETIME)<br />
END</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8211;Select the values using left outer joins on the week number<br />
&#8211;and the day number&#8230;<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
INSERT INTO @table(	ID_week,<br />
			Sunday,<br />
			Monday,<br />
			Tuesday,<br />
			Wednesday,<br />
			Thursday,<br />
			Friday,<br />
			Saturday)</p>
<p>SELECT	Ref.ID_week	AS ID_week,<br />
                	su.ID_datetime 	AS Sunday,<br />
                	m.ID_datetime  	AS Monday,<br />
                	tu.ID_datetime 	AS Tuesday,<br />
                	we.ID_datetime 	AS Wednesday,<br />
                	th.ID_datetime 	AS Thurday,<br />
                	fr.ID_datetime 	AS Friday,<br />
		sa.ID_datetime	AS Saturday<br />
FROM<br />
(SELECT DISTINCT 	ID_week<br />
FROM 			@t) 		AS Ref<br />
LEFT OUTER JOIN @t 			AS su<br />
ON 	su.ID_week 			= Ref.ID_week<br />
AND 	DATEPART(dw,su.ID_datetime) 	= 1<br />
LEFT OUTER JOIN @t 			AS m<br />
ON 	m.ID_week 			= Ref.ID_week<br />
AND 	DATEPART(dw,m.ID_datetime) 	= 2<br />
LEFT OUTER JOIN @t 			AS tu<br />
ON 	tu.ID_week			 = Ref.ID_week<br />
AND 	DATEPART(dw,tu.ID_datetime) 	= 3<br />
LEFT OUTER JOIN @t 			AS we<br />
ON 	we.ID_week 			= Ref.ID_week<br />
AND 	DATEPART(dw,we.ID_datetime) 	= 4<br />
LEFT OUTER JOIN @t 			AS th<br />
ON 	th.ID_week 			= Ref.ID_week<br />
AND 	DATEPART(dw,th.ID_datetime) 	= 5<br />
LEFT OUTER JOIN @t 			AS fr<br />
ON 	fr.ID_week 			= Ref.ID_week<br />
AND 	DATEPART(dw,fr.ID_datetime) 	= 6<br />
LEFT OUTER JOIN @t 			AS sa<br />
ON 	sa.ID_week 			= Ref.ID_week<br />
AND 	DATEPART(dw,sa.ID_datetime) 	= 7</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8211;Return the table<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
RETURN</p>
<p>END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Manas</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-43747</link>
		<dc:creator><![CDATA[Manas]]></dc:creator>
		<pubDate>Thu, 16 Oct 2008 17:54:28 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-43747</guid>
		<description><![CDATA[I fixed it; I believe it just for case sensitive, the first latter of column name must be same as you declare on table ,

thanks]]></description>
		<content:encoded><![CDATA[<p>I fixed it; I believe it just for case sensitive, the first latter of column name must be same as you declare on table ,</p>
<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-40938</link>
		<dc:creator><![CDATA[Doug]]></dc:creator>
		<pubDate>Tue, 29 Jul 2008 01:54:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-40938</guid>
		<description><![CDATA[There is an Error in this Code...try the date &#039;8/1/2008&#039; it drops the &#039;8/31/2008&#039;.
Here is the fix

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER FUNCTION [dbo].[GetSQLcalendar](@WhatDate SMALLDATETIME)
RETURNS @WeekDateDay TABLE
(
Sunday SMALLDATETIME,
Monday SMALLDATETIME,
Tuesday SMALLDATETIME,
Wednesday SMALLDATETIME,
Thursday SMALLDATETIME,
Friday SMALLDATETIME,
Saturday SMALLDATETIME
)
AS
BEGIN
/*
Purpose: To return a calendar fOR whatever MONTH you want, driven by the date you pick AS a variable.
By: Dan Golden
On: 2/20/2008
*/
DECLARE @FourWeeksAgo SMALLDATETIME
SET @FourWeeksAgo = DATEADD(d,-28,@WhatDate)
DECLARE @ThreeWeeksAgo SMALLDATETIME
SET @ThreeWeeksAgo = DATEADD(d,-21,@WhatDate)
DECLARE @TwoWeeksAgo SMALLDATETIME
SET @TwoWeeksAgo = DATEADD(d,-14,@WhatDate)
DECLARE @PreviousWeek SMALLDATETIME
SET @PreviousWeek = DATEADD(d,-7,@WhatDate)
DECLARE @ThisWeek SMALLDATETIME
SET @ThisWeek = (@WhatDate)
DECLARE @NextWeek SMALLDATETIME
SET @NextWeek = DATEADD(d,7,@WhatDate)
DECLARE @LastWeek SMALLDATETIME
SET @LastWeek = DATEADD(d,14,@WhatDate)
DECLARE @AfterLastWeek SMALLDATETIME
SET @AfterLastWeek = DATEADD(d,21,@WhatDate)
DECLARE @TwoAfterLastWeek SMALLDATETIME
SET @TwoAfterLastWeek = DATEADD(d,28,@WhatDate)
DECLARE @ThreeAfterLastWeek SMALLDATETIME
SET @ThreeAfterLastWeek = DATEADD(d,35,@WhatDate)
INSERT INTO @WeekDateDay (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
SELECT *
FROM
(
SELECT CASE MONTH(calendar.Sunday)
WHEN MONTH(@WhatDate)
THEN calendar.Sunday
ELSE NULL END AS &#039;Sunday&#039;,
CASE MONTH(calendar.Monday)
WHEN MONTH(@WhatDate)
THEN calendar.Monday
ELSE NULL END AS &#039;Monday&#039;,
CASE MONTH(calendar.Tuesday)
WHEN MONTH(@WhatDate)
THEN calendar.Tuesday
ELSE NULL END AS &#039;Tuesday&#039;,
CASE MONTH(calendar.Wednesday)
WHEN MONTH(@WhatDate)
THEN calendar.Wednesday
ELSE NULL END AS &#039;Wednesday&#039;,
CASE MONTH(calendar.Thursday)
WHEN MONTH(@WhatDate)
THEN calendar.Thursday
ELSE NULL END AS &#039;Thursday&#039;,
CASE MONTH(calendar.Friday)
WHEN MONTH(@WhatDate)
THEN calendar.Friday
ELSE NULL END AS &#039;Friday&#039;,
CASE MONTH(calendar.Saturday)
WHEN MONTH(@WhatDate)
THEN calendar.Saturday
ELSE NULL END AS &#039;Saturday&#039;
FROM
(
SELECT *
FROM dbo.DisplayCurrentWeekDays(@FourWeeksAgo)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@ThreeWeeksAgo)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@TwoWeeksAgo)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@PreviousWeek)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@ThisWeek)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@NextWeek)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@LastWeek)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@AfterLastWeek)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@TwoAfterLastWeek)
UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@ThreeAfterLastWeek)
) calendar
) dates
WHERE dates.sunday IS NOT NULL
OR dates.monday IS NOT NULL
OR dates.tuesday IS NOT NULL
OR dates.wednesday IS NOT NULL
OR dates.thursday IS NOT NULL
OR dates.friday IS NOT NULL
OR dates.saturday IS NOT NULL
RETURN
END



BTW I Added

DECLARE @ThreeAfterLastWeek SMALLDATETIME
SET @ThreeAfterLastWeek = DATEADD(d,35,@WhatDate)

AND

UNION ALL
SELECT *
FROM dbo.DisplayCurrentWeekDays(@ThreeAfterLastWeek)

IF anyone cares]]></description>
		<content:encoded><![CDATA[<p>There is an Error in this Code&#8230;try the date &#8217;8/1/2008&#8242; it drops the &#8217;8/31/2008&#8242;.<br />
Here is the fix</p>
<p>set ANSI_NULLS ON<br />
set QUOTED_IDENTIFIER ON<br />
go</p>
<p>ALTER FUNCTION [dbo].[GetSQLcalendar](@WhatDate SMALLDATETIME)<br />
RETURNS @WeekDateDay TABLE<br />
(<br />
Sunday SMALLDATETIME,<br />
Monday SMALLDATETIME,<br />
Tuesday SMALLDATETIME,<br />
Wednesday SMALLDATETIME,<br />
Thursday SMALLDATETIME,<br />
Friday SMALLDATETIME,<br />
Saturday SMALLDATETIME<br />
)<br />
AS<br />
BEGIN<br />
/*<br />
Purpose: To return a calendar fOR whatever MONTH you want, driven by the date you pick AS a variable.<br />
By: Dan Golden<br />
On: 2/20/2008<br />
*/<br />
DECLARE @FourWeeksAgo SMALLDATETIME<br />
SET @FourWeeksAgo = DATEADD(d,-28,@WhatDate)<br />
DECLARE @ThreeWeeksAgo SMALLDATETIME<br />
SET @ThreeWeeksAgo = DATEADD(d,-21,@WhatDate)<br />
DECLARE @TwoWeeksAgo SMALLDATETIME<br />
SET @TwoWeeksAgo = DATEADD(d,-14,@WhatDate)<br />
DECLARE @PreviousWeek SMALLDATETIME<br />
SET @PreviousWeek = DATEADD(d,-7,@WhatDate)<br />
DECLARE @ThisWeek SMALLDATETIME<br />
SET @ThisWeek = (@WhatDate)<br />
DECLARE @NextWeek SMALLDATETIME<br />
SET @NextWeek = DATEADD(d,7,@WhatDate)<br />
DECLARE @LastWeek SMALLDATETIME<br />
SET @LastWeek = DATEADD(d,14,@WhatDate)<br />
DECLARE @AfterLastWeek SMALLDATETIME<br />
SET @AfterLastWeek = DATEADD(d,21,@WhatDate)<br />
DECLARE @TwoAfterLastWeek SMALLDATETIME<br />
SET @TwoAfterLastWeek = DATEADD(d,28,@WhatDate)<br />
DECLARE @ThreeAfterLastWeek SMALLDATETIME<br />
SET @ThreeAfterLastWeek = DATEADD(d,35,@WhatDate)<br />
INSERT INTO @WeekDateDay (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)<br />
SELECT *<br />
FROM<br />
(<br />
SELECT CASE MONTH(calendar.Sunday)<br />
WHEN MONTH(@WhatDate)<br />
THEN calendar.Sunday<br />
ELSE NULL END AS &#8216;Sunday&#8217;,<br />
CASE MONTH(calendar.Monday)<br />
WHEN MONTH(@WhatDate)<br />
THEN calendar.Monday<br />
ELSE NULL END AS &#8216;Monday&#8217;,<br />
CASE MONTH(calendar.Tuesday)<br />
WHEN MONTH(@WhatDate)<br />
THEN calendar.Tuesday<br />
ELSE NULL END AS &#8216;Tuesday&#8217;,<br />
CASE MONTH(calendar.Wednesday)<br />
WHEN MONTH(@WhatDate)<br />
THEN calendar.Wednesday<br />
ELSE NULL END AS &#8216;Wednesday&#8217;,<br />
CASE MONTH(calendar.Thursday)<br />
WHEN MONTH(@WhatDate)<br />
THEN calendar.Thursday<br />
ELSE NULL END AS &#8216;Thursday&#8217;,<br />
CASE MONTH(calendar.Friday)<br />
WHEN MONTH(@WhatDate)<br />
THEN calendar.Friday<br />
ELSE NULL END AS &#8216;Friday&#8217;,<br />
CASE MONTH(calendar.Saturday)<br />
WHEN MONTH(@WhatDate)<br />
THEN calendar.Saturday<br />
ELSE NULL END AS &#8216;Saturday&#8217;<br />
FROM<br />
(<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@FourWeeksAgo)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@ThreeWeeksAgo)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@TwoWeeksAgo)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@PreviousWeek)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@ThisWeek)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@NextWeek)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@LastWeek)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@AfterLastWeek)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@TwoAfterLastWeek)<br />
UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@ThreeAfterLastWeek)<br />
) calendar<br />
) dates<br />
WHERE dates.sunday IS NOT NULL<br />
OR dates.monday IS NOT NULL<br />
OR dates.tuesday IS NOT NULL<br />
OR dates.wednesday IS NOT NULL<br />
OR dates.thursday IS NOT NULL<br />
OR dates.friday IS NOT NULL<br />
OR dates.saturday IS NOT NULL<br />
RETURN<br />
END</p>
<p>BTW I Added</p>
<p>DECLARE @ThreeAfterLastWeek SMALLDATETIME<br />
SET @ThreeAfterLastWeek = DATEADD(d,35,@WhatDate)</p>
<p>AND</p>
<p>UNION ALL<br />
SELECT *<br />
FROM dbo.DisplayCurrentWeekDays(@ThreeAfterLastWeek)</p>
<p>IF anyone cares</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver Kanp(Microsoft)</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-39547</link>
		<dc:creator><![CDATA[Oliver Kanp(Microsoft)]]></dc:creator>
		<pubDate>Fri, 27 Jun 2008 09:37:49 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-39547</guid>
		<description><![CDATA[--Solution For Mithun Ghosh

Function of the millennium


Create FUNCTION [dbo].[DisplayCurrentWeekDays]
    (@today SMALLDATETIME)
RETURNS @WeekDateDay TABLE
    (
				Saturday SMALLDATETIME,
                Sunday SMALLDATETIME,
                Monday SMALLDATETIME,
                Tuesday SMALLDATETIME,
                Wednesday SMALLDATETIME,
                Thursday SMALLDATETIME,
                Friday SMALLDATETIME
                
    )
            AS
    BEGIN
    DECLARE @day INT
        SET @today = CAST(CONVERT(VARCHAR(10), @today, 101) AS SMALLDATETIME)
        SET @day = DATEPART(dw, @today)
    INSERT INTO @WeekDateDay (Saturday,Sunday, Monday,
                    Tuesday, Wednesday, Thursday, Friday )
        SELECT  
				DATEADD(dd, 0 - @day, @today) Saturday,
				DATEADD(dd, 1 - @day, @today) Sunday,
                DATEADD(dd, 2 - @day, @today) Monday,
                DATEADD(dd, 3 - @day, @today) Tuesday,
                DATEADD(dd, 4 - @day, @today) Wednesday,
                DATEADD(dd, 5 - @day, @today) Thursday,
                DATEADD(dd, 6 - @day, @today) Friday
				
                
    RETURN
    END
------------------------------------------------------------

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


Create FUNCTION [dbo].[GetSQLcalendar](@WhatDate SMALLDATETIME)
RETURNS @WeekDateDay TABLE
    (
                Friday SMALLDATETIME
    )
            AS
    BEGIN
                  DECLARE @FourWeeksAgo SMALLDATETIME
        SET @FourWeeksAgo = DATEADD(d,-28,@WhatDate)
    DECLARE @ThreeWeeksAgo SMALLDATETIME
        SET @ThreeWeeksAgo = DATEADD(d,-21,@WhatDate)
    DECLARE @TwoWeeksAgo SMALLDATETIME
        SET @TwoWeeksAgo = DATEADD(d,-14,@WhatDate)
    DECLARE @PreviousWeek SMALLDATETIME
        SET @PreviousWeek = DATEADD(d,-7,@WhatDate)
    DECLARE @ThisWeek SMALLDATETIME
        SET @ThisWeek = (@WhatDate)
    DECLARE @NextWeek SMALLDATETIME
        SET @NextWeek = DATEADD(d,7,@WhatDate)
    DECLARE @LastWeek SMALLDATETIME
        SET @LastWeek = DATEADD(d,14,@WhatDate)
    DECLARE @AfterLastWeek SMALLDATETIME
        SET @AfterLastWeek = DATEADD(d,21,@WhatDate)
    DECLARE @TwoAfterLastWeek SMALLDATETIME
        SET @TwoAfterLastWeek = DATEADD(d,28,@WhatDate)
 INSERT INTO @WeekDateDay (Friday)
        SELECT *
        FROM
        (
        SELECT 
            CASE MONTH(calendar.Friday)
                        WHEN MONTH(@WhatDate)
                        THEN calendar.Friday
            ELSE NULL END AS &#039;Friday&#039;
            FROM
            (
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@FourWeeksAgo)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@ThreeWeeksAgo)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@TwoWeeksAgo)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@PreviousWeek)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@ThisWeek)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@NextWeek)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@LastWeek)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@AfterLastWeek)
                UNION ALL
            SELECT *
                FROM dbo.DisplayCurrentWeekDays(@TwoAfterLastWeek)
            ) calendar
        ) dates
 WHERE dates.friday IS NOT NULL

    RETURN
    END

---------------------------------------------------------

USE AdventureWorks;
GO
DECLARE @today SMALLDATETIME
SELECT @today = &#039;7/4/2008&#039;; 
--SELECT		ISNULL(CONVERT(NVARCHAR(20), Saturday, 101), &#039;&#039;) AS &#039;Saturday&#039;,	
--			ISNULL(CONVERT(NVARCHAR(20), Sunday, 101), &#039;&#039;) AS &#039;Sunday&#039;,
--            ISNULL(CONVERT(NVARCHAR(20), Monday, 101), &#039;&#039;) AS &#039;Monday&#039;,
--            ISNULL(CONVERT(NVARCHAR(20), Tuesday, 101), &#039;&#039;) AS &#039;Tuesday&#039;,
--            ISNULL(CONVERT(NVARCHAR(20), Wednesday, 101), &#039;&#039;) AS &#039;Wednesday&#039;,
--            ISNULL(CONVERT(NVARCHAR(20), Thursday, 101), &#039;&#039;) AS &#039;Thursday&#039;,
     SELECT       ISNULL(CONVERT(NVARCHAR(20), Friday, 101), &#039;&#039;) AS &#039;Friday&#039;
          
    FROM dbo.GetSQLcalendar(@today)
GO
--------------------------------------------------------------------------
--------------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



Create Procedure proc_weekendingdate
 @WhatDate smalldatetime

AS
SET NOCOUNT ON
 
SELECT ISNULL(CONVERT(NVARCHAR(20), Friday, 107), &#039;&#039;) AS &#039;WeekEndingDate&#039;

FROM GetSQLcalendar(@WhatDate)

GO

exec proc_weekendingdate
&#039;6/27/2008&#039;



--------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

Create Procedure proc_CurrentWeekDate
 @CurrentDate smalldatetime

AS
SET NOCOUNT ON


--SELECT @today = &#039;7/4/2008&#039;; 
SELECT		ISNULL(CONVERT(NVARCHAR(20), Saturday,007), &#039;&#039;) AS &#039;Saturday&#039;,
			ISNULL(CONVERT(NVARCHAR(20), Sunday, 007), &#039;&#039;) AS &#039;Sunday&#039;,
            ISNULL(CONVERT(NVARCHAR(20), Monday, 007), &#039;&#039;) AS &#039;Monday&#039;,
            ISNULL(CONVERT(NVARCHAR(20), Tuesday, 007), &#039;&#039;) AS &#039;Tuesday&#039;,
            ISNULL(CONVERT(NVARCHAR(20), Wednesday,007), &#039;&#039;) AS &#039;Wednesday&#039;,
            ISNULL(CONVERT(NVARCHAR(20), Thursday,007), &#039;&#039;) AS &#039;Thursday&#039;,
		    ISNULL(CONVERT(NVARCHAR(20), Friday, 007), &#039;&#039;) AS &#039;Friday&#039;
			
          
    FROM dbo.DisplayCurrentWeekDays(@CurrentDate)
GO

exec proc_CurrentWeekDate
 &#039;7/3/2008&#039;]]></description>
		<content:encoded><![CDATA[<p>&#8211;Solution For Mithun Ghosh</p>
<p>Function of the millennium</p>
<p>Create FUNCTION [dbo].[DisplayCurrentWeekDays]<br />
    (@today SMALLDATETIME)<br />
RETURNS @WeekDateDay TABLE<br />
    (<br />
				Saturday SMALLDATETIME,<br />
                Sunday SMALLDATETIME,<br />
                Monday SMALLDATETIME,<br />
                Tuesday SMALLDATETIME,<br />
                Wednesday SMALLDATETIME,<br />
                Thursday SMALLDATETIME,<br />
                Friday SMALLDATETIME</p>
<p>    )<br />
            AS<br />
    BEGIN<br />
    DECLARE @day INT<br />
        SET @today = CAST(CONVERT(VARCHAR(10), @today, 101) AS SMALLDATETIME)<br />
        SET @day = DATEPART(dw, @today)<br />
    INSERT INTO @WeekDateDay (Saturday,Sunday, Monday,<br />
                    Tuesday, Wednesday, Thursday, Friday )<br />
        SELECT<br />
				DATEADD(dd, 0 &#8211; @day, @today) Saturday,<br />
				DATEADD(dd, 1 &#8211; @day, @today) Sunday,<br />
                DATEADD(dd, 2 &#8211; @day, @today) Monday,<br />
                DATEADD(dd, 3 &#8211; @day, @today) Tuesday,<br />
                DATEADD(dd, 4 &#8211; @day, @today) Wednesday,<br />
                DATEADD(dd, 5 &#8211; @day, @today) Thursday,<br />
                DATEADD(dd, 6 &#8211; @day, @today) Friday</p>
<p>    RETURN<br />
    END<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>set ANSI_NULLS ON<br />
set QUOTED_IDENTIFIER ON<br />
go</p>
<p>Create FUNCTION [dbo].[GetSQLcalendar](@WhatDate SMALLDATETIME)<br />
RETURNS @WeekDateDay TABLE<br />
    (<br />
                Friday SMALLDATETIME<br />
    )<br />
            AS<br />
    BEGIN<br />
                  DECLARE @FourWeeksAgo SMALLDATETIME<br />
        SET @FourWeeksAgo = DATEADD(d,-28,@WhatDate)<br />
    DECLARE @ThreeWeeksAgo SMALLDATETIME<br />
        SET @ThreeWeeksAgo = DATEADD(d,-21,@WhatDate)<br />
    DECLARE @TwoWeeksAgo SMALLDATETIME<br />
        SET @TwoWeeksAgo = DATEADD(d,-14,@WhatDate)<br />
    DECLARE @PreviousWeek SMALLDATETIME<br />
        SET @PreviousWeek = DATEADD(d,-7,@WhatDate)<br />
    DECLARE @ThisWeek SMALLDATETIME<br />
        SET @ThisWeek = (@WhatDate)<br />
    DECLARE @NextWeek SMALLDATETIME<br />
        SET @NextWeek = DATEADD(d,7,@WhatDate)<br />
    DECLARE @LastWeek SMALLDATETIME<br />
        SET @LastWeek = DATEADD(d,14,@WhatDate)<br />
    DECLARE @AfterLastWeek SMALLDATETIME<br />
        SET @AfterLastWeek = DATEADD(d,21,@WhatDate)<br />
    DECLARE @TwoAfterLastWeek SMALLDATETIME<br />
        SET @TwoAfterLastWeek = DATEADD(d,28,@WhatDate)<br />
 INSERT INTO @WeekDateDay (Friday)<br />
        SELECT *<br />
        FROM<br />
        (<br />
        SELECT<br />
            CASE MONTH(calendar.Friday)<br />
                        WHEN MONTH(@WhatDate)<br />
                        THEN calendar.Friday<br />
            ELSE NULL END AS &#8216;Friday&#8217;<br />
            FROM<br />
            (<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@FourWeeksAgo)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@ThreeWeeksAgo)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@TwoWeeksAgo)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@PreviousWeek)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@ThisWeek)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@NextWeek)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@LastWeek)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@AfterLastWeek)<br />
                UNION ALL<br />
            SELECT *<br />
                FROM dbo.DisplayCurrentWeekDays(@TwoAfterLastWeek)<br />
            ) calendar<br />
        ) dates<br />
 WHERE dates.friday IS NOT NULL</p>
<p>    RETURN<br />
    END</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>USE AdventureWorks;<br />
GO<br />
DECLARE @today SMALLDATETIME<br />
SELECT @today = &#8217;7/4/2008&#8242;;<br />
&#8211;SELECT		ISNULL(CONVERT(NVARCHAR(20), Saturday, 101), &#8221;) AS &#8216;Saturday&#8217;,<br />
&#8211;			ISNULL(CONVERT(NVARCHAR(20), Sunday, 101), &#8221;) AS &#8216;Sunday&#8217;,<br />
&#8211;            ISNULL(CONVERT(NVARCHAR(20), Monday, 101), &#8221;) AS &#8216;Monday&#8217;,<br />
&#8211;            ISNULL(CONVERT(NVARCHAR(20), Tuesday, 101), &#8221;) AS &#8216;Tuesday&#8217;,<br />
&#8211;            ISNULL(CONVERT(NVARCHAR(20), Wednesday, 101), &#8221;) AS &#8216;Wednesday&#8217;,<br />
&#8211;            ISNULL(CONVERT(NVARCHAR(20), Thursday, 101), &#8221;) AS &#8216;Thursday&#8217;,<br />
     SELECT       ISNULL(CONVERT(NVARCHAR(20), Friday, 101), &#8221;) AS &#8216;Friday&#8217;</p>
<p>    FROM dbo.GetSQLcalendar(@today)<br />
GO<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
set ANSI_NULLS ON<br />
set QUOTED_IDENTIFIER ON<br />
go</p>
<p>Create Procedure proc_weekendingdate<br />
 @WhatDate smalldatetime</p>
<p>AS<br />
SET NOCOUNT ON</p>
<p>SELECT ISNULL(CONVERT(NVARCHAR(20), Friday, 107), &#8221;) AS &#8216;WeekEndingDate&#8217;</p>
<p>FROM GetSQLcalendar(@WhatDate)</p>
<p>GO</p>
<p>exec proc_weekendingdate<br />
&#8217;6/27/2008&#8242;</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
set ANSI_NULLS ON<br />
set QUOTED_IDENTIFIER ON<br />
go</p>
<p>Create Procedure proc_CurrentWeekDate<br />
 @CurrentDate smalldatetime</p>
<p>AS<br />
SET NOCOUNT ON</p>
<p>&#8211;SELECT @today = &#8217;7/4/2008&#8242;;<br />
SELECT		ISNULL(CONVERT(NVARCHAR(20), Saturday,007), &#8221;) AS &#8216;Saturday&#8217;,<br />
			ISNULL(CONVERT(NVARCHAR(20), Sunday, 007), &#8221;) AS &#8216;Sunday&#8217;,<br />
            ISNULL(CONVERT(NVARCHAR(20), Monday, 007), &#8221;) AS &#8216;Monday&#8217;,<br />
            ISNULL(CONVERT(NVARCHAR(20), Tuesday, 007), &#8221;) AS &#8216;Tuesday&#8217;,<br />
            ISNULL(CONVERT(NVARCHAR(20), Wednesday,007), &#8221;) AS &#8216;Wednesday&#8217;,<br />
            ISNULL(CONVERT(NVARCHAR(20), Thursday,007), &#8221;) AS &#8216;Thursday&#8217;,<br />
		    ISNULL(CONVERT(NVARCHAR(20), Friday, 007), &#8221;) AS &#8216;Friday&#8217;</p>
<p>    FROM dbo.DisplayCurrentWeekDays(@CurrentDate)<br />
GO</p>
<p>exec proc_CurrentWeekDate<br />
 &#8217;7/3/2008&#8242;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mithun Ghosh</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-39542</link>
		<dc:creator><![CDATA[Mithun Ghosh]]></dc:creator>
		<pubDate>Fri, 27 Jun 2008 04:53:32 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-39542</guid>
		<description><![CDATA[How to retrieve the weekending dates of a particular month and year .
 we need the stored procedures for this work where month and year are passed as parameters.

e.g : if @month=May
       
            @year=2008
 
          Then the weekending dates should return 2-May-2008,

9-May-2008,16-May-2008,23-May-2008,30-May-2008.]]></description>
		<content:encoded><![CDATA[<p>How to retrieve the weekending dates of a particular month and year .<br />
 we need the stored procedures for this work where month and year are passed as parameters.</p>
<p>e.g : if @month=May</p>
<p>            @year=2008</p>
<p>          Then the weekending dates should return 2-May-2008,</p>
<p>9-May-2008,16-May-2008,23-May-2008,30-May-2008.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elias Horne</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-38740</link>
		<dc:creator><![CDATA[Elias Horne]]></dc:creator>
		<pubDate>Thu, 22 May 2008 20:28:26 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-38740</guid>
		<description><![CDATA[Hello,

I tried this particular code and seem to have found an error.  I tried using 2007-12-01 00:00:00.000 as a date and it stopped at 12/29/2007.  No Sunday 12/30/2007 or Monday 12/31/2007.

Any ideas what is causing this or what I might be doing wrong?]]></description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I tried this particular code and seem to have found an error.  I tried using 2007-12-01 00:00:00.000 as a date and it stopped at 12/29/2007.  No Sunday 12/30/2007 or Monday 12/31/2007.</p>
<p>Any ideas what is causing this or what I might be doing wrong?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan Flodin</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-34014</link>
		<dc:creator><![CDATA[Jan Flodin]]></dc:creator>
		<pubDate>Wed, 27 Feb 2008 07:33:18 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-34014</guid>
		<description><![CDATA[Thanks a lot. I will use it. One question though. I would like to use &quot;SET DATEFIRST 1&quot; which means Monday will be the first day in the week for me. Where will I typically put the datefirst statement?

Regards,

Jan Flodin]]></description>
		<content:encoded><![CDATA[<p>Thanks a lot. I will use it. One question though. I would like to use &#8220;SET DATEFIRST 1&#8243; which means Monday will be the first day in the week for me. Where will I typically put the datefirst statement?</p>
<p>Regards,</p>
<p>Jan Flodin</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-33942</link>
		<dc:creator><![CDATA[pinaldave]]></dc:creator>
		<pubDate>Mon, 25 Feb 2008 05:16:34 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-33942</guid>
		<description><![CDATA[Hello Phil Factor,

Thank you for posting your comment. I appreciate your suggestion. You are one of my favorite author.

Regards,
Pinal Dave (http://www.SQLAuthority.com)]]></description>
		<content:encoded><![CDATA[<p>Hello Phil Factor,</p>
<p>Thank you for posting your comment. I appreciate your suggestion. You are one of my favorite author.</p>
<p>Regards,<br />
Pinal Dave (<a href="http://www.SQLAuthority.com" rel="nofollow">http://www.SQLAuthority.com</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phil Factor</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-33931</link>
		<dc:creator><![CDATA[phil Factor]]></dc:creator>
		<pubDate>Sun, 24 Feb 2008 10:03:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-33931</guid>
		<description><![CDATA[Howdy Jeff,

I don&#039;t know how useful this particular one is, but the one I wrote way back on Simple-talk has been adapted to a couple of live applications. In one case it turned into a Golfing Event diary for an application Robyn and I wrote. It also formed the bedrock for a reporting application where trends over the day of the week and week of the month was important to the business (telephony)

http://www.simple-talk.com/community/blogs/philfactor/archive/2006/01/10/117.aspx

it is quite handy to have this sort of application in TSQL where you are subsequently going to pin data onto it.]]></description>
		<content:encoded><![CDATA[<p>Howdy Jeff,</p>
<p>I don&#8217;t know how useful this particular one is, but the one I wrote way back on Simple-talk has been adapted to a couple of live applications. In one case it turned into a Golfing Event diary for an application Robyn and I wrote. It also formed the bedrock for a reporting application where trends over the day of the week and week of the month was important to the business (telephony)</p>
<p><a href="http://www.simple-talk.com/community/blogs/philfactor/archive/2006/01/10/117.aspx" rel="nofollow">http://www.simple-talk.com/community/blogs/philfactor/archive/2006/01/10/117.aspx</a></p>
<p>it is quite handy to have this sort of application in TSQL where you are subsequently going to pin data onto it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Moden</title>
		<link>http://blog.sqlauthority.com/2008/02/20/sql-server-udf-to-return-a-calendar-for-any-date-for-any-year/#comment-33920</link>
		<dc:creator><![CDATA[Jeff Moden]]></dc:creator>
		<pubDate>Sun, 24 Feb 2008 02:08:19 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=512#comment-33920</guid>
		<description><![CDATA[Well howdy...

Everytime I see one of these calendar generators, I have to say... sure, they make an interesting exercise... but what good are they and what have YOU actually used one for?

Thanks,
--Jeff Moden]]></description>
		<content:encoded><![CDATA[<p>Well howdy&#8230;</p>
<p>Everytime I see one of these calendar generators, I have to say&#8230; sure, they make an interesting exercise&#8230; but what good are they and what have YOU actually used one for?</p>
<p>Thanks,<br />
&#8211;Jeff Moden</p>
]]></content:encoded>
	</item>
</channel>
</rss>

