How do I exclude Weekend days in a SQL Server query?
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)
SELECT date_created
FROM your_table
WHERE DATENAME(dw, date_created) NOT IN ('Saturday', 'Sunday')
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
Try the DATENAME()
function:
select [date_created]
from table
where DATENAME(WEEKDAY, [date_created]) <> 'Saturday'
and DATENAME(WEEKDAY, [date_created]) <> 'Sunday'