Click to See Complete Forum and Search --> : DATEDIFF & excluding weekends


themoon
06-23-2008, 09:03 AM
Hi
I have this query the fetches all records which have not been updated since the last 2days. I am having schedule task that sends email for those records.
now, here is the trick, how can i exclude weekends from the query if the weekends is on Thursday & Friday ?

SELECT *,DATEDIFF(day, date_recorded, getdate()) AS no_of_days
FROM tablename
WHERE DATEDIFF(day, date_recorded, getdate()) <= '2'


date_recorded is a field name in my table.

Thanks
M

sstalder
06-23-2008, 12:07 PM
I believe this would work:

SELECT *,DATEDIFF(day, date_recorded, getdate()) AS no_of_days
FROM tablename
WHERE DATEDIFF(day, date_recorded, getdate()) <= '2' AND DAYOFWEEK(date_recorded) != 4 AND DAYOFWEEK(date_recorded) != 5

themoon
06-24-2008, 03:13 AM
Hi sstalder,
Thanks for your reply.
Sorry, I have n't mentioned that i am using mssql server.
The function 'DAYOFWEEK' is mysql function.
Now i used datepart after i set the first day of the to week to Saturday
as following:

SET DATEFIRST 6
SELECT *,DATEDIFF(day, date_recorded, getdate()) AS no_of_days
FROM tablename
WHERE DATEDIFF(day, date_recorded, getdate()) <= '2'
AND DATEPART(dw, date_recorded) != '6'
AND DATEPART(dw, date_recorded) != '7'

Regards
M