Udostępnij za pośrednictwem


What was the date on Last Monday?

One of my friend asked me logic/Query to find date of last Monday.

Everyday of the week can be identified as a number i.e. Sunday is 1, Monday is 2 and so on...

Since he wanted to find out date on Monday, it is 2nd day of week. Below is the logic

Monday Of The Week = Given date - (Day number of given week - 2)

We are using 2 because we want to find Monday. So the SQL query would be..

declare     @date datetime

set         @date = getdate()-10 -- I have take any date

select      @date - (DatePart(dw, @date)-2) -- This is the logic which I explained earlier

Have Fun!!!

 

Note: This depends on what the datefirst variable is set to. This would work only if DateFirst is 7 which is the U.S.English default.

Comments