user3067476
user3067476

Reputation: 37

MySQL how to get Month and Year with separation like date format

I have "datetime" field on my database, but what I want is only take YEAR-MONTH e.g ("2012-02")

I've tried using function YEAR_MONTH but the output without "-" which is similar like "201202".

Is there any way I could make that select?

Upvotes: 2

Views: 4815

Answers (4)

Punitha Subramani
Punitha Subramani

Reputation: 1477

You want to give False in second Param in Select

select("DATE_FORMAT(now(), '%Y-%m') AS dated_now", FALSE);

Upvotes: 1

Jishnu Janardhanan
Jishnu Janardhanan

Reputation: 63

Try this

SELECT DATE_FORMAT(now(),'%Y %m');

You can also check the below link

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Upvotes: 0

devbetter
devbetter

Reputation: 21

Try this:

SELECT DATE_FORMAT(now(),'%Y-%m');

Upvotes: 1

Mureinik
Mureinik

Reputation: 311228

You can use the date_format function to specify how to format your columns:

SELECT DATE_FORMAT(my_datetime_field, '%Y-%m')
FROM   my_table

Upvotes: 2

Related Questions