First date of a month in MySql

278
Views
1
Answers

How to get first date of a month by passing current date or a specified date?

edit | flag
William Patrick
Asked on: Aug 05, 2011 at 9:33PM

1 Answers

53
4
53

Try this..

SELECT DATE_SUB(now(),INTERVAL (DAY(now())-1) DAY);

this gives output like

2011-08-01 12:05:36

If you need just the date wrap it up in a date function like this

SELECT date(DATE_SUB(now(),INTERVAL (DAY(now())-1) DAY) );

The output will be like

2011-08-01

To get the first date of a specific date

SELECT DATE_SUB('2010-09-10',INTERVAL (DAY('2010-09-10')-1) DAY) 

which gives the output like

2010-09-01

Cheers!!

edit | flag
Gokul A
Answered on: Aug 05, 2011 at 9:39PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags