Given a datetime
, is there a way we can know it happens to be a Saturday
or Sunday
.
Any ideas and suggestions are appreciated!
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');
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.
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.
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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With