0

As I am writing a stored procedure which will returns the first day and last day of a number. for example , if I input 2, it will returns 1-2-2013 and 28-2-2013, if I input 7 , then it will return 1-7-2013 and 31-7-2013. which 2013 is current year. Can I do that?

Thanks

Peter Hon
  • 27
  • 1
  • 2
  • 10
  • Yes. If you post some code we may be able to suggest how. –  Aug 08 '13 at 03:54
  • See the answers to this question, they show how to get first and last day. http://stackoverflow.com/questions/3298288/how-to-get-first-day-of-every-corresponding-month-in-mysql – bumperbox Aug 08 '13 at 03:57

2 Answers2

0

To get the beginning of a month for the current year (the example uses month 2):

SET @theMonth = 2;
SET @beginMonth = STR_TO_DATE(CONCAT('1-', @theMonth, '-', YEAR(CURDATE())), '%d-%m-%Y');

To calculate the end of the month starting with the beginning of the month, add one month and then subtract one day:

SET @endMonth = @beginMonth + INTERVAL 1 MONTH - INTERVAL 1 DAY;
Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69
0

As the first day of any month is 1, you can use this to get the last day by using the appropriately named LAST_DAY function:

mysql> SELECT LAST_DAY(STR_TO_DATE(CONCAT(YEAR(CURDATE()),'-','2-1'),GET_FORMAT(DATE,'ISO')));
+---------------------------------------------------------------------------------+
| LAST_DAY(STR_TO_DATE(CONCAT(YEAR(CURDATE()),'-','2-1'),GET_FORMAT(DATE,'ISO'))) |
+---------------------------------------------------------------------------------+
| 2013-02-28                                                                      |
+---------------------------------------------------------------------------------+
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284