How To Determine which days are weekends

  • Hi Guys,

    I have written a view that consists of data from a table called Tasks. Basically each task has a start date, end date, estimated end date, estimated hours, actual hours complete.

    Now I calculate the estimated end date like this:

    I divide the number of estimated hours by 2. Then add the result to the Start Date using the DateAdd function.(my boss came up with that algorithm)

    However, I need to determine which of the days between the Start Date and Estimated End Date are weekends. If there is a weekend I need to add two days more to the estimated end date.

    Please help me achieve this.

  • You can use DATEPART to check the weekday:

    select datepart(weekday , getdate())

    Depending on your @@DATEFIRST setting, you will have to check for days 1 and 7 (default for US English) or 6 and 7 (default for Italian).

    Hope this helps

    Gianluca

    -- Gianluca Sartori

  • This expression is independent of any language and datefirst settings:

    -- gives 1 for monday..., 7 for sunday

    datepart( weekday, @datetime + @@datefirst - 1 )

    _____________________________________________________
    Microsoft Certified Master: SQL Server 2008
    XDetails Addin - for SQL Developers
    blog.sqlxdetails.com - Transaction log myths
  • Vedran Kesegic (6/3/2011)


    This expression is independent of any language and datefirst settings:

    -- gives 1 for monday..., 7 for sunday

    datepart( weekday, @datetime + @@datefirst - 1 )

    This is a really smart solution! Saved it in my code snippets. 🙂

    Thanks a lot.

    -- Gianluca Sartori

  • Hi,

    There is no harm in usung this

    select datename(dw,getdate())

    and can check for Sat or Sun accordingly.

    Regards

    Ashok

  • ashok.faridabad1984 (6/7/2011)


    Hi,

    There is no harm in usung this

    select datename(dw,getdate())

    and can check for Sat or Sun accordingly.

    Regards

    Ashok

    It still depends on the client's language:

    set language italian

    select datename(dw,getdate())

    Saturday is called "Sabato" here.

    -- Gianluca Sartori

Viewing 6 posts - 1 through 5 (of 5 total)

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