﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Ashka Modi  / What were the dates of all the Fridays in this quarter? / 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 09:12:19 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>My point of view is if you can use "BETWEEN AND" for comparing dates, avoid &gt;,&lt; all such symbols. Isnt it true?</description><pubDate>Tue, 25 Aug 2009 22:32:15 GMT</pubDate><dc:creator>Ashka Modi</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>Here you go, try this. You'll need a Tally table first (see [url=http://www.sqlservercentral.com/articles/T-SQL/62867/][u]here[/u][/url]):[code="sql"]DECLARE @startDate datetime, @endDate DATETIMESELECT @startDate = '20090101', -- First day in range       @endDate = '20090401'    -- Day *after* last day   --==   --== Note that my tally table starts at 1 hence the N-1 below   --== SELECT         DATEADD(dd,n-1,@startDate)   FROM         Tally  WHERE         N &lt;= DATEDIFF(dd,@startDate,@endDate) AND      --==   --== Adding the day of the week to the @@DATEFIRST value then doing MOD 7   --== normalizes the result to be independent of the DATEFIRST setting   --== 6 just happens to be Friday   --==        (DATEPART(dw,DATEADD(dd,n-1,@startDate)) + @@DATEFIRST) % 7 = 6 [/code]You could put it in a stored procedure with the start date, end date and day of week you're interested in as parameters.It isn't all that different from using a calendar table. But you seemed concerned about the storage requirements. A Tally table would  use less space.There are other ways without using a tally table - common table expressions and ROW_NUMBER are just two (if you're using 2005 or above). Nigel</description><pubDate>Tue, 25 Aug 2009 08:06:13 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>Agreed, those extra little enhancements are really useful in most situations, and customizable as well.Different countries having different public holidays is a good example.One-time Setup and Simple Selects were the alternatives i was interested in proposing</description><pubDate>Tue, 25 Aug 2009 07:53:23 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>Ashka, to answer number 4.  With a Calendar table you can use ...[code]Select * from CalendarWhere Q=3 and Y=2009 and dw=5 [/code]to get all of the Fridays in the Quarter.On my server this takes on average .093 of a second compared to your loop taking about .145Not a huge amount of time, but every little bit may count.  My calendar table also has other functions - I have Fiscal weeks and months along with calendar, frequently I need to connect to a AS/400 database, and it uses a different date format, it's easier and faster to link the data to my calendar table than use a function.  I include holidays and workdays to get a count of shipping days in a quarter.  There's a lot of flexibility and power that you can't get from a loop.Mike Webster</description><pubDate>Tue, 25 Aug 2009 07:48:10 GMT</pubDate><dc:creator>mwebster-1117730</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>[quote][b]Ashka Modi (8/25/2009)[/b][hr]2) Use of cursor is never been a good idea. cursor includes lot of overhead to query. if you can use if else or for loop just avois cursor.[/quote]I would avoid all looping constructs in SQL and try and come up with an alternative set-based solution.I'll be back soon with a suitable alternative method.[quote]4) what i have learnt is datetime things should never be compared like this: dt &gt;= '20040401' AND dt &lt; '20040501'. This is not efficient way of coding.[/quote]Why? What is your reasoning? What are the alternatives?</description><pubDate>Tue, 25 Aug 2009 07:43:12 GMT</pubDate><dc:creator>nigel.</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>I am so sorry for assume it is your post. Please never mind it. I would like more suggestion and more abstract version of it. what i think is SQL is about making complex logic. and this is tiny piece of it.</description><pubDate>Tue, 25 Aug 2009 06:25:33 GMT</pubDate><dc:creator>Ashka Modi</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>Not actually my article, but one I found.The idea behind it is to incur a single , perhaps large, set-up cost and from then on just use the set-based capabilites that SQLServer is designed for.Thanks for other the observations , will take them into consideration next time I implement one of these auxiliary calendar tablescheers</description><pubDate>Tue, 25 Aug 2009 06:16:55 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>Well what you designed is too good. What i created is part of script and to practice it with datetime function with minimum loops included. While browsing your article i found something which i would like to share with you. I am not much good with sql but all these are my queries:1) you come up new fashioned query to add large amount of data in table. but what about memory size it will occupy? I have worked on calendar module developed in asp.net, havent seen like created calendar table with all dates in database. If you consider real case scenario this isnt possible.2) Use of cursor is never been a good idea. cursor includes lot of overhead to query. if you can use if else or for loop just avois cursor.3) with use of small functions, you can implement all features. you can use table variable instead.4) what i have learnt is datetime things should never be compared like this: dt &gt;= '20040401' AND dt &lt; '20040501'. This is not efficient way of coding.I hope you understand my point of view.</description><pubDate>Tue, 25 Aug 2009 05:54:36 GMT</pubDate><dc:creator>Ashka Modi</dc:creator></item><item><title>RE: What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>Is there a reason why this would be better than selecting rows from a Calendar table?Have a look at:http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html</description><pubDate>Tue, 25 Aug 2009 02:56:13 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>What were the dates of all the Fridays in this quarter?</title><link>http://www.sqlservercentral.com/Forums/Topic773129-1626-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/DateTime/67901/"&gt;What were the dates of all the Fridays in this quarter?&lt;/A&gt;[/B]</description><pubDate>Tue, 18 Aug 2009 17:03:59 GMT</pubDate><dc:creator>Ashka Modi</dc:creator></item></channel></rss>