-1

I am trying to extract the month from the date string 2011-11-29 19:30:00 using MySQL.

I have tried EXTRACT(MONTH FROM date) as d to no avail, and also STR_TO_DATE(EXTRACT(MONTH FROM date), '%Y-%m-%d') AS d also with no joy.

Can anyone suggest the proper method to extract the month from such a string and group by it?

5
  • Where does the string come from? Is it a DATETIME column or a VARCHAR? And what was your result with EXTRACT? Jan 22, 2013 at 11:02
  • It's a string so not a DATETIME variable, just a VARCHAR. Wouldn't of thought it would have mattered what variation of text field it was beyond it wasn't an actual mysql date structure?
    – Gga
    Jan 22, 2013 at 11:06
  • a DATETIME is not a text field, so yes, it should matter. Try STR_TO_DATE Jan 22, 2013 at 11:08
  • @fab that is what I said, and what I put in my answer 'beyond it wasn't an actual mysql date'...
    – Gga
    Jan 22, 2013 at 11:11
  • It was not clear from your question, that's why I asked. But I see you found the STR_TO_DATE solution by yourself, great :) Jan 22, 2013 at 11:46

4 Answers 4

2

you can use

 select DATE_FORMAT('2011-11-29 19:30:00', '%c');
1

SUBSTRING(date_string,6,2)?

x

1
EXTRACT(MONTH FROM STR_TO_DATE(date_ordered, '%Y-%m-%d')) as month

seemed to work well

0

You can get day from datetime field as like following

DATE_FORMAT( date , '%d' ) AS d

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.