How to calculate number of weeks worked by an employee for a fiscal year in sql server

Maddi, Shyam 0 Reputation points
2024-02-02T20:23:26.1633333+00:00

How to determine the total weeks an employee has worked during a fiscal year using SQL Server. My start date is 12-28-2023 and till today, I would like to calculate the no.of weeks worked by an employee during that time.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,179 questions
SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
62 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,591 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 50,506 Reputation points
    2024-02-02T21:28:18.2466667+00:00

    Have you tried using DATEDIFF?

    DECLARE @start DATETIME, @end DATETIME
    SET @start = '12/28/2023'
    SET @end = '12/27/2024'
    
    SELECT DATEDIFF(week, @start, @end)
    

    A caveat here is that weeks worked isn't well defined. This is the # of weeks between 2 dates but might not line up with what you consider to be work weeks. For example it doesn't take into account holidays or weekends. If you need that then the calculation will get a lot more complicated as weekends need to be removed and holidays are not a fixed list.

    1 person found this answer helpful.
    0 comments No comments