如何仅在日期字段中插入年和月? [英] How to insert just year and month into date field?

查看:56
本文介绍了如何仅在日期字段中插入年和月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 table_date 的列,目前我正在使用 now() 插入当前日期 (2011-02-23).我知道我可以用 sql/php 操作它来显示年和月名.但是,我想知道是否可以像这样 2011-02 那样将当前日期作为年月插入 table_date 中?谢谢

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

推荐答案

DATE 字段总是会是一个完整的日期,据我所知,它也总是需要指定一个完整的日期.

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.

使用 01 可能是最简单的,例如 2011-02-01 用于 2011 年 2 月.

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

显然,您可以在查询时根据自己的喜好格式化 DATE 字段的输出:

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

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

这篇关于如何仅在日期字段中插入年和月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆