For example, my starting day of the week is Monday (2/1/2011), when user input is (4/1/2011) Wednesday, is there any built-in functions that can help me get the starting date of the current week?
Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming that SQL server’s
DATEFIRSTsetting fits in with your idea of how weeks are aligned (this probably only matters for Saturdays, Sundays, etc), then a DATEADD/DATEDIFF pair from an arbitrary Monday should give the result you want:Where
CURRENT_TIMESTAMPobviously picks todays date. If you were selecting from a table (Tab), with a column calledRandomDate, then you might do:But I’m confused by your example, since neither the 2nd of January nor the 1st of February (usual interpretation possibilities for ‘2/1/2011’) is a Monday. The random Monday I selected was
'20110103'– so if, instead, you needed to find the friday for a particular date, you’d use'20110107'in both places where'20110103'appears