How to insert only year and month in the date field?

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

+4
source share
2 answers
Field

A DATE will always be a full date, and as far as I know, you always need to specify the full date.

The easiest way is to use 01 , for example 2011-02-01 for February 2011.

Obviously, you can format the output of the DATE field as you wish when prompted:

 SELECT DATE_FORMAT(fieldname,"%Y-%m"); 
+6
source

If you type something like 2010-10 in the DATE column, mysql will issue a warning and insert 0000-00-00 . If you do not want to indicate a specific day, you can insert something like 2010-10-00 . Pay attention to the query "all records in October 2010", since WHERE date >= '2010-10-01' will not return 2010-10-00 .

+5
source

Source: https://habr.com/ru/post/1340959/


All Articles