Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert just year and month into date field?

Tags:

mysql

I have a column called table_date which currently I am using now() to insert the current date (2011-02-23). I know I can manipulate this with sql/php to show me year and monthname. However, I want to know if it's possible to just insert into table_date the current date as year-month like this 2011-02? Thanks

like image 690
Jonathan Avatar asked Feb 23 '11 12:02

Jonathan


1 Answers

A DATE field is always going to be a full date, and as far as I know, it is also always required to specify a full date.

It might be easiest to use 01, like 2011-02-01 for February 2011.

Obviously, you can format the output of a DATE field to your liking when querying it:

SELECT DATE_FORMAT(fieldname,"%Y-%m");
like image 65
Pekka Avatar answered Nov 03 '22 22:11

Pekka