Guest User

Untitled

a guest
Oct 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. SELECT DATE_ADD(`date_field`, INTERVAL 5 DAY) As FinalDate
  2. FROM `table_name`;
  3.  
  4. SELECT DATE_ADD(
  5. date_field,
  6. INTERVAL 5 +
  7. IF(
  8. (WEEK(date_field) <> WEEK(DATE_ADD(date_field, INTERVAL 5 DAY)))
  9. OR (WEEKDAY(DATE_ADD(date_field, INTERVAL 5 DAY)) IN (5, 6)),
  10. 2,
  11. 0)
  12. DAY
  13. ) AS FinalDate
  14. FROM `table_name`;
  15.  
  16. SELECT
  17. @ID:='2017-10-04' as initial_date, -- the initial date in the right format to manipulate (add x day)
  18. @DTA:=20 as days_to_add, -- number of days to add
  19. @DA:= DATE_ADD(@ID, INTERVAL @DTA DAY) as date_add,
  20. @LASTDAY := WEEKDAY(@DA) as last_day, -- the day (Monday, Tuesday...) corresponding to the initial date + number of days to add
  21. @WEEK1 := DATE_FORMAT(@ID, '%v') as initial_date_week, -- format the initial date to match week mode 3 (Monday 1-53)
  22. @WEEK2 := DATE_FORMAT(@DA, '%v') as added_date_week_nbr, -- the week # of the initial_date + number of days to add
  23. @WEEKDIFF := @WEEK2 - @WEEK1 as week_difference, -- the difference between week 2 and week 1
  24. DATE_ADD(@ID,
  25. INTERVAL @DTA +
  26. if ( @WEEKDIFF > 0 or @LASTDAY in (5,6),
  27. 2,
  28. 0
  29. ) +
  30. if (@WEEKDIFF > 1,
  31. @WEEKDIFF*2,
  32. 0
  33. ) DAY
  34. ) AS FinalDate
Add Comment
Please, Sign In to add comment