MySQL日期格式 [英] MySQL date format

查看:119
本文介绍了MySQL日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个表并在mysql中创建一个类型date的字段时,它会将日期类似于0000-00-00。是否可以将格式更改为dm-Y?

When I make a table and create a field of type date in mysql, it stores date like 0000-00-00. Is it possible to change the format to 'd-m-Y'?

推荐答案

转到 MySQL参考 - 10.5。数据类型存储要求

搜索:日期和时间类型的存储要求

日期内部存储为一个三字节整数打包为DD + MM×32 + YYYY×16×32

但是,如果您选择显示的日期列,则必须以一些方式显示,因此它显示为0000-00-00 。将 存储为具有特定格式的字符(10)。

But, if you select a date column for display, it has to be shown in some way, so it comes out as 0000-00-00. It is not stored as a char(10) with that specific format.

如果显示,则需要查看特定格式,您可以使用 Date_Format()。但是,请记住,如果您在编程工具中使用查询,则这是 您要做什么。您希望生成日期值,为了显示,您将使用与所使用的任何编程环境相似的格式化功能。

If, for display, you need to see a specific format, you can convert it to VARCHAR in a specific format using Date_Format(). However, bear in mind that if you are using the query in a programming tool, this is not what you want to do. You want the raw date value, for display purposes, there will be a similar formatting function from whatever programming environment you use.

请参阅DATE_FORMAT中的参考,您将要使用 '%d-%m-%Y' ,例如

As you can see from the reference in DATE_FORMAT, you will want to use '%d-%m-%Y', e.g.

SELECT col1, col2, DATE_FORMAT(datecolumn, '%d-%m-%Y') AS datecolumn, more1...
FROM sometable
....

这篇关于MySQL日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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