How to Get the First Day of the Week as Sunday in SQL Server

Learn how to retrieve the first day of the week (Sunday) for any date using SQL Server with a simple DATEADD and DATEDIFF query.

37 views

In SQL Server, to get the first day of the week (assuming Sunday) for any given date, you can use the following query: `SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6)`. This code calculates the difference in weeks between a known Sunday (`DATEDIFF(wk, 6, GETDATE())`) and the current date (`GETDATE()`), then adds that number of weeks back to a known Sunday (`DATEADD(wk, <calculated number of weeks>, 6)`), effectively returning the first day of the current week.

FAQs & Answers

  1. How do I find the first day of the week in SQL Server? You can use a combination of DATEDIFF and DATEADD functions to calculate the first day of the week, typically using a known reference day such as Sunday or Monday.
  2. Why is Sunday considered the first day of the week in SQL Server? By default, SQL Server considers Sunday as the first day of the week depending on the DATEFIRST setting or regional settings, but it can be adjusted as needed.
  3. Can I change the first day of the week in SQL Server? Yes, you can change the first day of the week using the SET DATEFIRST command to set which day SQL Server treats as the start of the week.