How do I subtract 3 days from current date only when current day = 2, so that I retrieve the previous Fridays date?

5 pts.
Tags:
T-SQL
T-SQL 2008 R2
I am new to using T-SQL, so there is probably an easy way to accomplish this, but I can't seem to get it right.  I want to retrieve the previous day's date (requires a weekday) to use as a comparsioin to a date field in the table, format is YYYYMMDD.  When the current date is a Monday, I need to subtract 3 days from current date, otherwise subtract 1 day.

Software/Hardware used:
T-SQL

Answer Wiki

Thanks. We'll let you know when a new response is added.

Try this
<pre>select date,
DATEADD(dd,case when datename(dw, date) = ‘monday’ then -3 else -1 end, date)
from table</pre>

<a href=”http://www.sql-tutorial.ru/en/book_tsql_datetime_functions.html”>date/time functions</a>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following