0

I'm new to MySQL, I've a date variable inside my MySQL procedure and I'm trying to find week start date for that IN date variable. My procedure goes as below,

CREATE PROCEDURE my_proc (IN week_start_num INT, IN my_date DATE)
BEGIN
   DECLARE my_new_date DATE;
   #I know what I'm trying here is wrong
   SET my_new_date=startdate - (INTERVAL WEEKDAY( startdate  ) - week_start_num + IF( WEEKDAY( startdate  ) > week_start_num, 0, 7 ))

   #rest of my codes goes here

END

I know this is wrong 'SET my_new_date =startdate - (INTERVAL WEEKDAY( startdate ) - week_start_num + IF( WEEKDAY( startdate ) > week_start_num, 0, 7 ))', what is the correct way to accomplish it?

1 Answer 1

1

Try this:

SET my_new_date = startdate
                  - INTERVAL 
                  ( 
                    WEEKDAY(startdate)
                    - week_start_num
                    + IF(WEEKDAY(startdate) > week_start_num, 0, 7)
                  )
                  DAY;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.