Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first day of the week and last day of the week in sql server 2008?

How to get the first day of the week and last day of the week when we input any one day of a week?

For example if we enter a date then the first(Monday) and last (Friday) day should be displayed. that is if we enter 24-jan-2014 then 20-jan-2014 and 24-jan-2014 should be displayed.

Regards

like image 208
Gautam Seshadri Avatar asked Jan 24 '14 10:01

Gautam Seshadri


1 Answers

Here's how you can do it:

DECLARE @yourdate date = getdate()
Select dateadd(ww, datediff(ww, 0, @yourdate), 0)
Select dateadd(ww, datediff(ww, 0, @yourdate), 4)

You set @yourdate to the date you want. The first SELECT will give you the first day and the second SELECT will give you the last date

like image 151
Niklas Avatar answered Oct 26 '22 09:10

Niklas