given certain date, how to get the first day of previous month with the same weekday in mysql -


i'm noob using mysql, , search here , in google didn't me this.

i need obtain first day of previous month of date same weekday

example: given date: may 29th of 2015 (friday), need first friday of april (april 3rd of 2015).

(bear in mind, need value inside update (specifically, inside where))

thanks help! have nice day.

this shows 1 way there. if can't use or don't want intermediate variables, replace them repeated sql.

set @startdate= '2015-04-06'; set @firstdayofprevmon =  concat(year(date_add(@startdate, interval -1 month)),'-',lpad(month(date_add(@startdate, interval -1 month)),2,'0'),'-01' ) ;  select case  when dayofweek(@firstdayofprevmon) <  dayofweek(@startdate)  /* if day of week first day of previous month less target day of week,simply increment difference */  date_add(@firstdayofprevmon, interval (dayofweek(@startdate)-dayofweek(@firstdayofprevmon)) day)    when dayofweek(@firstdayofprevmon) >  dayofweek(@startdate)   /* if greater, add week difference */  date_add(@firstdayofprevmon, interval (dayofweek(@startdate)-dayofweek(@firstdayofprevmon) +7)  day)   else  /* otherwise, first day of month answer */  @firstdayofprevmon  end targetdate 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -