How to Get the First Monday of a Month Using SQL Query
Learn how to retrieve the first Monday of any month in SQL with a simple and effective query using DATEADD and DATEPART functions.
570 views
To get the first Monday of a month in SQL, use this query: `SELECT DATEADD(day, (7 - DATEPART(WEEKDAY, DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1)) + 1) % 7, DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1))` . Replace `GETDATE()` with the target date if needed.
FAQs & Answers
- How do I find the first Monday of a specific month in SQL? Use the SQL query combining DATEADD and DATEPART functions on the first day of the month to calculate the first Monday dynamically.
- Can the given SQL query be adjusted for other weekdays? Yes, by modifying the calculation offset in the query, you can find the first occurrence of any weekday within a month.
- What does DATEPART(WEEKDAY, date) return in SQL? DATEPART with the WEEKDAY parameter returns an integer representing the day of the week for the given date, typically from 1 (Sunday) to 7 (Saturday), depending on your SQL server settings.