SET @START_DATE = '2018-07-03'; SET @END_DATE = '2018-07-28'; select count_weekdays(@START_DATE, @END_DATE, 6) as Sundays, count_weekdays(@START_DATE, @END_DATE, 5) + count_weekdays(@START_DATE, @END_DATE, 6) as weekends; Where 6 ~ number of weekday stands for Sunday, 5 ~ Saturday.
10 Answers
Replace GETDATE() with a parameter @date to get the last Sunday before a particular date. If the actual END_OF_WEEK is next Sun-Sat week, then you need to +7 to this week's value. (See the END_OF_WEEK example above.)SQL Date Data Types
DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS. YEAR - format YYYY or YY.MySQL WEEKDAY() Function
The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.Each IF statement has a condition. If the condition evaluates to TRUE then the statement block in the IF clause is executed. If the condition is FALSE , then the code block in the ELSE clause is executed.
You Can simply use datediff function of sql. and then you can subtract weekends between those dates if any. For example check below query. And If You want to exclude holiday's too, then, You also can calculate holidays between start/end date and can subtract that from final selection.
Reading the code above is simple:
select the minimum calendar
date that's after today and that falls on
Monday.
This solution is based on following property of DATETIME type:
- Day 0 = 19000101 = Mon.
- Day 1 = 19000102 = Tue.
- Day 2 = 19000103 = Wed.
by suresh. The @@DATEFIRST in SQL Server is one of the Date and Time Function, which will return the first day of the week. This value is between 1 and 7. If your default language is US English, then by default, 7 is returned.
MySQL WEEKDAY() Function
The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.----Today SELECT GETDATE() 'Today' ----Yesterday SELECT DATEADD(d,-1,GETDATE()) 'Yesterday' ----First Day of Current Week SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) 'First Day of Current Week' ----Last Day of Current Week SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) 'Last Day of Current Week' ----First Day of Last
Here is the SQL: select "start_of_week" = dateadd(week, datediff(week, 0, getdate()), 0); This returns 2011-08-22 00:00:00.000 , which is a Monday, not a Sunday.
Then used DATEPART with week parameter to get the yearly week number for the given date and the first day of month. Finally subtracted the yearly week number of first day of the month from yearly week number of given date and added 1 to get the monthly week number of the given date.
Week 21 is from Monday, May 18, 2020 until (and including) Sunday, May 24, 2020. The highest week number in a year is either 52 or 53. 2020 has 53 weeks. ISO 8601 is not the only week numbering system in the world, other systems use weeks starting on Sunday (US) or Saturday (Islamic).
Step 1: Calculate total working days
- DECLARE @TotalWorkDays INT, @TotalTimeDiff DECIMAL(18, 2), @DateFrom DATETIME, @DateTo DATETIME;
- SET @TotalWorkDays = DATEDIFF(DAY, @DateFrom, @DateTo)
- -(DATEDIFF(WEEK, @DateFrom, @DateTo) * 2)
- WHEN DATENAME(WEEKDAY, @DateFrom) = 'Sunday'
- WHEN DATENAME(WEEKDAY, @DateTo) = 'Saturday'
[Time] < o. [Time] ) SELECT [empid], [Date], SUM(DATEDIFF(HOUR, [in], [out])) AS [Hours] FROM t WHERE r = 1 GROUP BY [empid], [Date] ORDER BY [empid], [Date] DROP TABLE #logs; It works by doing a self join where each side of the join is pre-filtered for the specific entry type you are looking for.