Given a datetime
, is there a way we can know it happens to be a Saturday
or Sunday
.
Any ideas and suggestions are appreciated!
Given a datetime
, is there a way we can know it happens to be a Saturday
or Sunday
.
Any ideas and suggestions are appreciated!
SELECT DATENAME(weekday, GetDate())
Check this for sql server: http://msdn.microsoft.com/en-US/library/ms174395(v=sql.90).aspx Check this for .net: http://msdn.microsoft.com/en-us/library/bb762911.aspx
This may generate wrong results, because the number produced by the weekday datepart depends on the value set by SET DATEFIRST. This sets the first day of the week. So another way is:
This expression
will always return a number between 0 and 6 where
Independently from
@@DATEFIRST
So a weekend day is tested like this
Many ways to do this, you can use DATENAME and check for the actual strings 'Saturday' or 'Sunday'
Or use the day of the week and check for 1 (Sunday) or 7 (Saturday)
ok i figure out :
than i use : .......... OR free=1
Attention: The other answers only work on SQL Servers with English configuration! Use
SET DATEFIRST 7
to ensureDATEPART(DW, ...)
returns 1 for Sunday and 7 for Saturday.Here's a version that is independent of the local setting and does not require to use :
If you don't want to use the function, simply use this in your SELECT statement: