I need the correct syntax to give me :
GETDATE()
GETDATE()
So, based on today's date (14/09/2012) I would want the following:
To get the date of the previous Monday: Add 6 to the day of the week and get the remainder of dividing by 7 . Subtract the result from the day of the month.
=A2-WEEKDAY(A2-2) Get First Monday Before Any Date in Excel. The Excel WORKDAY Function automatically returned 5)12/1980 as the first Monday occuring before the date in cell A2.
Easy:
--start of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0) --end of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6)
EDIT:
The below will handle the Sunday date issue.
DECLARE @input varchar(10) --SET @input = '9/9/2012' -- simulates a Sunday SET @input = GETDATE() --start of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, CASE DATEPART(dw,@input) WHEN 1 THEN DATEADD(d,-1,@input) ELSE @input END ), 0) --end of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, CASE DATEPART(dw,@input) WHEN 1 THEN DATEADD(d,-1,@input) ELSE @input END ), 6)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With