How do I exclude values in a DateTime
column that are Saturdays or Sundays?
For example, given the following data:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-28 09:00:00' -- Saturday
'2009-11-29 09:00:00' -- Sunday
'2009-11-30 09:00:00' -- Monday
this is the result I'm looking for:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-30 09:00:00' -- Monday
Thanks!
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...
Assuming you're using SQL Server, use DATEPART with dw:
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
The answer depends on your server's week-start set up, so it's either
if Sunday is the first day of the week for your server
or
if Monday is the first day of the week for your server
Comment if you've got any questions :-)
Try this code
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.