﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Gregory Jackson / Article Discussions / Article Discussions by Author  / What about MONTH and YEAR functions? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 11:25:56 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: What about MONTH and YEAR functions?</title><link>http://www.sqlservercentral.com/Forums/Topic462584-143-1.aspx</link><description>For too many reasons to list in this short amount of space, I strongly recommend that you [i][b]never [/b][/i]process dates by individual Year and/or Month components.  Some of the others have already suggested how to handle things... always treat dates as a range... yep... even if the date is for one measely day.  WHERE clauses should always follow the general format of...[code]  WHERE somedatecol &amp;gt;= @StartDate    AND somedatecol &amp;lt; @EndDate+1[/code]That is assuming, of course, that you are working with whole dates that either have no literally expressed time component (defaults to midnight) or has a time component of precisely 00:00:00.000.  Other considerations will need be made if @StartDate or @EndDate have a non-midnight time component.  Doesn't matter if "somedatecol" does or not and that's the beauty of the method shown above.  And, it'll allow for very high performance Index SEEKs if the correct indexes are available.You will also find those that suggest that you use one of the following...[code]  WHERE somedatecol BETWEEN @StartDate AND DATEADD(ms,-3,@EndDate+1)  WHERE somedatecol BETWEEN @StartDate AND @EndDate+'23:59:59.997[/code]Treat them just like street drugs... just say "NO".  ;)  Do they work?  Yes, today they do... when 2008 comes out, it will "depend".  ;)</description><pubDate>Fri, 29 Feb 2008 12:20:31 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: What about MONTH and YEAR functions?</title><link>http://www.sqlservercentral.com/Forums/Topic462584-143-1.aspx</link><description>The thing about functions in where clauses is that, if on a column, you have to run every row through the function while using them on a parameter or constant the optimizer can run it once.  This is why Michael's and Lynn's solutions are more effecient.</description><pubDate>Fri, 29 Feb 2008 12:15:37 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: What about MONTH and YEAR functions?</title><link>http://www.sqlservercentral.com/Forums/Topic462584-143-1.aspx</link><description>here is a slight variation to what Michael Valentine Jones provided:[code]declare @year smallint,        @month tinyintset @year = 2007set @month = 2select    sum(Amount)from    dbo.myEntrieswhere    myEntries.myDate &amp;gt;= dateadd(yy,(@year - 1900), 0)    and myEntries.myDate &amp;lt; dateadd(mm, @month, dateadd(yy,(@year - 1900), 0))[/code]:cool:</description><pubDate>Fri, 29 Feb 2008 12:05:15 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: What about MONTH and YEAR functions?</title><link>http://www.sqlservercentral.com/Forums/Topic462584-143-1.aspx</link><description>If this is how you will most often be querying your data you should have month and year columns in table with an index. Or, write your queries with explicit date ranges. If you want your users to be able to enter month and year paramters then convert them to dates after entry. So year=2008 and month=3 would become. Startdate=1/1/2008 &amp; endate=3/31/2008.</description><pubDate>Fri, 29 Feb 2008 11:46:43 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: What about MONTH and YEAR functions?</title><link>http://www.sqlservercentral.com/Forums/Topic462584-143-1.aspx</link><description>Do queries like this as a selection against a range in this form:where date &amp;gt;= @StartOfRange and date &amp;lt; @EndOf RangeIn the following code, it will be the same as this:where date &amp;gt;= '2008-01-01' and date &amp;lt; '2008-03-01'[code]declare @year intdeclare @month intselect @year = 2008, @month = 2select	*from	MyTablewhere	-- Date on or after start of year	MyTable.MyDate &amp;gt;= dateadd(month,(12*@Year)-22801+1,0)	and	-- Date before start of next month	MyTable.MyDate &amp;lt;  dateadd(month,(12*@Year)-22801+@Month+1,0)[/code]</description><pubDate>Fri, 29 Feb 2008 11:43:50 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>What about MONTH and YEAR functions?</title><link>http://www.sqlservercentral.com/Forums/Topic462584-143-1.aspx</link><description>Hi, First of all thank you, it's a good point to be considered. In fact, I use functions on the WHERE clause very often, specially when filtering dates ... how do you optimize a query like this:  SELECT SUM(Amount) FROM myEntries WHERE YEAR(myEntries.myDate) = @Year AND MONTH(myEntries.myDate) &amp;lt;= @MonthAny clue? thank you regards</description><pubDate>Fri, 29 Feb 2008 11:06:16 GMT</pubDate><dc:creator>ruben ruvalcaba</dc:creator></item></channel></rss>