What were the dates of all the Fridays in this quarter?

  • Ashka Modi

    Old Hand

    Points: 377

    Comments posted to this topic are about the item What were the dates of all the Fridays in this quarter?

    Thanks,

    Ashka Modi
    Software Engineer || credEcard Technologies (india) Pvt. Ltd.

  • Pete Cox

    SSChasing Mays

    Points: 636

    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

  • Ashka Modi

    Old Hand

    Points: 377

    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 >= '20040401' AND dt < '20040501'. This is not efficient way of coding.

    I hope you understand my point of view.

    Thanks,

    Ashka Modi
    Software Engineer || credEcard Technologies (india) Pvt. Ltd.

  • Pete Cox

    SSChasing Mays

    Points: 636

    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 tables

    cheers

  • Ashka Modi

    Old Hand

    Points: 377

    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.

    Thanks,

    Ashka Modi
    Software Engineer || credEcard Technologies (india) Pvt. Ltd.

  • nigel.

    SSChampion

    Points: 11592

    Ashka Modi (8/25/2009)


    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.

    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.

    4) what i have learnt is datetime things should never be compared like this: dt >= '20040401' AND dt < '20040501'. This is not efficient way of coding.

    Why? What is your reasoning? What are the alternatives?

  • mwebster-1117730

    SSC Enthusiast

    Points: 115

    Ashka, to answer number 4. With a Calendar table you can use ...

    Select * from Calendar

    Where Q=3 and Y=2009 and dw=5

    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 .145

    Not 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

  • Pete Cox

    SSChasing Mays

    Points: 636

    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

  • nigel.

    SSChampion

    Points: 11592

    Here you go, try this. You'll need a Tally table first (see here[/url]):

    DECLARE @startDate datetime, @endDate DATETIME

    SELECT @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 <= 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

    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

  • Ashka Modi

    Old Hand

    Points: 377

    My point of view is if you can use "BETWEEN AND" for comparing dates, avoid >,< all such symbols. Isnt it true?

    Thanks,

    Ashka Modi
    Software Engineer || credEcard Technologies (india) Pvt. Ltd.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply