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

angularjs - Redirect to a new template in angular js -

Oracle SQL. How to select specific IDs where value of columns specify criteria? -

Desktop Launcher for Python Script Starts Program in Wrong Path (Linux) -