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

前端 未结 7 2455
[愿得一人]
[愿得一人] 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条回答
  •  Happy的楠姐
    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)
    

提交回复
热议问题