How do I exclude Weekend days in a SQL Server query?

前端 未结 7 2400
[愿得一人]
[愿得一人] 2020-11-27 03:20

How do I exclude values in a DateTime column that are Saturdays or Sundays?

For example, given the following data:

date_created
\'2009-1         


        
相关标签:
7条回答
  • 2020-11-27 04:02

    Calculate Leave working days in a table column as a default value--updated

    If you are using SQL here is the query which can help you: http://gallery.technet.microsoft.com/Calculate...

    0 讨论(0)
  • 2020-11-27 04:05

    Assuming you're using SQL Server, use DATEPART with dw:

    SELECT date_created
    FROM your_table
    WHERE DATEPART(dw, date_created) NOT IN (1, 7);
    

    EDIT: I should point out that the actual numeric value returned by DATEPART(dw) is determined by the value set by using SET DATEFIRST:
    http://msdn.microsoft.com/en-us/library/ms181598.aspx

    0 讨论(0)
  • 2020-11-27 04:07

    The answer depends on your server's week-start set up, so it's either

    SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (7,1)
    

    if Sunday is the first day of the week for your server

    or

    SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (6,7)
    

    if Monday is the first day of the week for your server

    Comment if you've got any questions :-)

    0 讨论(0)
  • 2020-11-27 04:08

    Try this code

    select (DATEDIFF(DD,'2014-08-01','2014-08-14')+1)- (DATEDIFF(WK,'2014-08-01','2014-08-14')* 2)
    
    0 讨论(0)
  • 2020-11-27 04:10

    When dealing with day-of-week calculations, it's important to take account of the current DATEFIRST settings. This query will always correctly exclude weekend days, using @@DATEFIRST to account for any possible setting for the first day of the week.

    SELECT *
    FROM your_table
    WHERE ((DATEPART(dw, date_created) + @@DATEFIRST) % 7) NOT IN (0, 1)
    
    0 讨论(0)
  • 2020-11-27 04:14
    SELECT date_created
    FROM your_table
    WHERE DATENAME(dw, date_created) NOT IN ('Saturday', 'Sunday')
    
    0 讨论(0)
提交回复
热议问题