Calculate weekend

  • Hi,

    I have a scenario in which i need to create a function which takes 2 parameter (count,startdate).

    If I have a count of 30 and start date is 1/1/2012 then 30 should get added to atsrt date and it will return 31/1/2012.

    No i need to caculate no. of sundays between 1/1/2012 and 31/1/2012. Suppose i have 4 sundays then i need to add 4in date 31/1/2012 which will result in 4/2/2012 , now again i have to calcualte that is there any sunday between 31/1/2012 and 4/2/2012 and so on.........

    plz help me on this...

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Take a look at this article by Lynn Pettis to see if that helps: http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

    Rob

  • I'm not quite sure what you are asking for here. Are you trying to add 30 days to a given date such that you get a date x days later but skips Saturdays/Sundays?

  • Based on the original post, I'm thinking you want something like this:

    declare @TestDate date = '20120101',

    @DaysToAdd int = 8;

    select

    @TestDate,

    @DaysToAdd,

    dateadd(dd, @DaysToAdd, @TestDate),

    datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)),

    dateadd(dd, datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)) - 1, dateadd(dd, @DaysToAdd, @TestDate));

  • yes you are right, I am trying to add 30 to a given date to get a x later date and want to count sunday in that date range and thn again add count of those sunday in that x date...

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • the script you have written is not satisfy the scenario that i wrote..

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • kapil_kk (12/2/2012)


    the script you have written is not satisfy the scenario that i wrote..

    You are going to have to tell me more than that it doesn't work. If I give it 2012-01-01 and 30 days, it returns 2012-02-04, just like you posted.

    Maybe it doesn't satisify your actual requirements, but you really aren't giving us much to work with. We need the DDL (CREATE TABLE) statement for your table, some sample data as a seried of INSERT INTO statements, and the expected results based on the sample data. With that information you will get a better answer.

  • Lynn Pettis (12/2/2012)


    kapil_kk (12/2/2012)


    the script you have written is not satisfy the scenario that i wrote..

    You are going to have to tell me more than that it doesn't work. If I give it 2012-01-01 and 30 days, it returns 2012-02-04, just like you posted.

    Maybe it doesn't satisify your actual requirements, but you really aren't giving us much to work with. We need the DDL (CREATE TABLE) statement for your table, some sample data as a seried of INSERT INTO statements, and the expected results based on the sample data. With that information you will get a better answer.

    This appears to work if you stay in the same year:

    declare @TestDate date = '20120201',

    @DaysToAdd int = 30;

    select

    @TestDate,

    @DaysToAdd,

    dateadd(dd, @DaysToAdd, @TestDate),

    datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)),

    datepart(wk,@TestDate),

    dateadd(dd, datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)) - datepart(wk,@TestDate), dateadd(dd, @DaysToAdd, @TestDate));

  • As I think about this more, your apparent requirement is that everytime you cross Sunday you need to add an additional day, correct?

    Example, if the startdate is 2012-11-30 (a Friday) and you add 3 days you would normally get 2012-12-03 (a Monday). What you want, however, is to get 2012-12-04 (a Tuesday). Is this correct?

    Edit: And you probably want this to work over a year boundary as well, correct?

  • Another question, startdate is 2012-12-02 (a Sunday) and you add 1 day, do you want 2012-12-03 or 2012-12-04? What if you add 0 days to the same date? Will you every add a negative number of days (i.e. go backwards from a given date)?

  • in this case it will be '2012-02-03'

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • I have created this script but looking for more optimization regarding performance--

    DECLARE @count int

    SET @count = 30

    DECLARE @sundays int

    SET @sundays = 0

    DECLARE @startdate datetime

    SET @startdate ='2012-01-01'

    DECLARE @enddate datetime

    SET @enddate = DATEADD(DD,@count,@startdate)

    PRINT @enddate

    PRINT 'Before Sunday'

    WHILE @startdate <= @enddate

    BEGIN

    IF DATEPART(DW,@startdate) = 1

    BEGIN

    SET @sundays = @sundays + 1

    SET @enddate = DATEADD(DD,1,@enddate)

    PRINT @enddate

    PRINT 'After Sunday'

    PRINT @sundays

    END

    SET @startdate = DATEADD(DD,1,@startdate)

    END

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Using a Tally Table would improve this code significantly, also you have to be careful about using the fucntion datepart(dw,a_date) as in some cases Sunday isnt always represented by 1, and will depend on your regional settings.

    Eg : In the US, DW 1 is Sunday, yet in europe it can be DW 1 is monday.

    See books on line http://msdn.microsoft.com/en-us/library/ms174420.aspx

    To mitigate this you need to use the SET DATEFIRST to make it consistent across all regional settings, see http://msdn.microsoft.com/en-us/library/ms181598.aspx

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • Anyone know if Joe's query

    SELECT (C2.julian_business_nbr - C1.julian_business_nbr)

    FROM Calendar AS C1, Calendar AS C2

    WHERE C1.cal_date = '2007-04-05',

    AND C2.cal_date = '2007-04-10';

    causes a cross join before the complete WHERE clause is applied ?

    Or are we just cross joining the 2 rows that would be the answer sets of 2 discreet queries, each using one of the phrases of the where clause?

  • Jason-299789 (12/3/2012)


    Using a Tally Table would improve this code significantly, also you have to be careful about using the fucntion datepart(dw,a_date) as in some cases Sunday isnt always represented by 1, and will depend on your regional settings.

    Eg : In the US, DW 1 is Sunday, yet in europe it can be DW 1 is monday.

    See books on line http://msdn.microsoft.com/en-us/library/ms174420.aspx

    To mitigate this you need to use the SET DATEFIRST to make it consistent across all regional settings, see http://msdn.microsoft.com/en-us/library/ms181598.aspx

    Jason,

    I know it sounds strange to those of us that use a Tally Table on a regular basis, but there are still a lot of folks that don't know what it is. You either have to explain, provide a link, or provide the full code (preferably some combination of those) for people that don't understand.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 15 posts - 1 through 15 (of 28 total)

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