Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if datetime happens to be Saturday or Sunday in SQL Server 2008

Given a datetime, is there a way we can know it happens to be a Saturday or Sunday.

Any ideas and suggestions are appreciated!

like image 401
SaiBand Avatar asked Mar 05 '12 16:03

SaiBand


People also ask

How do you check a date is Saturday or Sunday in SQL?

Use the DATENAME() function and specify the datepart as weekday . select ID, Name, Salary, Date from dbo. yourTable where datename(weekday, Date) in ('Saturday', 'Sunday');

How do I get Sundays in SQL?

Option 1: Sunday as the First Day of the WeekDATEADD(week, DATEDIFF(week, -1, RegistrationDate), -1) AS Sunday; The function DATEADD() takes three arguments: a datepart, a number, and a date.

How do I find weekends in SQL Server?

SQL SERVER – UDF – Get the Day of the Week Function The day of the week can be retrieved in SQL Server by using the DatePart function. The value returned by function is between 1 (Sunday) and 7 (Saturday). To convert this to a string representing the day of the week, use a CASE statement.

How do you check if a date is Friday in SQL?

select datediff(day, '1/1/2000', getdate())%7; If that is 0, the date is a Saturday, 1 = Sunday, 2 = Monday, 3 = Tuesday, etc. Since it's possible to change a server setting and change how datepart(weekday) works, using the mathematical method is more certain.


1 Answers

Many ways to do this, you can use DATENAME and check for the actual strings 'Saturday' or 'Sunday'

SELECT DATENAME(DW, GETDATE()) 

Or use the day of the week and check for 1 (Sunday) or 7 (Saturday)

SELECT DATEPART(DW, GETDATE()) 
like image 81
msmucker0527 Avatar answered Sep 20 '22 16:09

msmucker0527