Click here to Skip to main content
15,880,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,

How to get week start date in SQL queries, if i am entering any date.

suppose i select 5-Dec-13, i want to display week start date(30-Nov-13) based on input date. means,i want saturday as week start date(30-Nov-13) to end date friday(5-Dec-13).

only i want to dispaly week start date based on user input date in SQL Queries.
Zoltán Zörgő 13-Dec-13 11:39am    
Any progress?

The sample you had given is quite unclear, but have a look here:

declare @d datetime
set @d='2013-12-05'

DATEADD(dd, -(DATEPART(dw, @d)-1), @d) [WeekStart], 
DATEADD(dd, 7-(DATEPART(dw, @d)), @d) [WeekEnd]

It will return 2013-12-01 (Sunday) as week start, and 2013-12-07 (Saturday) as week end. If you want custom week start, play with the constants.

Here is the modified version that makes a week to start on Saturday and end on Friday:
DATEADD(dd, -(DATEPART(dw, @d)), @d) [WeekStart], 
DATEADD(dd, 6-(DATEPART(dw, @d)), @d) [WeekEnd]
Share this answer
Mike Meinz 7-Dec-13 8:40am    
+5Great answer!
To get the date of the day that is the start of the week, here are three examples:

Replace GetDate() with your date column name or date value.

Select DateAdd(wk, DateDiff(wk, 0, GetDate()) - 1, 0) as LastWeekStartDate
Select DateAdd(wk, DateDiff(wk, 0, GetDate()), 0) as ThisWeekStartDate
Select DateAdd(wk, DateDiff(wk, 0, GetDate()) + 1, 0) as NextWeekStartDate

Note: The result will depend on the SQL Server's @@DATEFIRST parameter.
Share this answer
The DATEADD function simply allows you to add or subtract the specified number of units of time to a specified date/time value.

The format of the DATEADD function is as follows:

DATEADD(<Unit of time>, <Units>, <Date>)

-- to add 5 days to September 1, 2011 the function would be
DATEADD(DAY, 5, '9/1/2011')

-- to subtract 5 months from September 1, 2011 the function would be
DATEADD(MONTH, -5, '9/1/2011')

select DATEADD(DAY,-5, '12/5/2013') as WeakStartDate
Share this answer
Zoltán Zörgő 7-Dec-13 8:15am    
Won't be helpful on it's own :(
Mahesh Bailwal 7-Dec-13 8:27am    
Can you please tell me what was wrong with this solution.
Zoltán Zörgő 7-Dec-13 8:31am    
It is working only for some of the inputs. You substract a constant! See my post.
Mahesh Bailwal 7-Dec-13 8:47am    
Sorry, my intention was to give just any idea about "DATEADD" in SQL not the exact answer. :(
Zoltán Zörgő 7-Dec-13 8:49am    
Than it is not an answer - OP won't be able to solve the problem just with that. The missing part is the trick. You could have posted this as a comment.
You can use DateTime.DayOfWeek to come to the beginning of the week. DayOfWeek starts with 0 for Sunday, so yuo have to take your special beginning of the week into acount.

Something like this:

DateTime dt = DateTime.Now;
dt.AddDays(-((int)dt.DayOfWeek + 1));
Share this answer
Zoltán Zörgő 7-Dec-13 8:15am    
Read topic tags: OP wants result in T-SQL!
[no name] 7-Dec-13 8:20am    
Uups, you are right, sorry...But maybe it gives OP an idea.
This is called also interdisciplinary... at least I see for you it gave the input to answer it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900