更改格式日期MySql [英] changing format date MySql

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

问题描述

我在MySql中有日期类型列. 默认情况下,MySql日期格式为YYYY/MM/DD. 但实际上我更喜欢使用DD/MM/YYYY格式.

i have date type column in MySql. by default MySql date format is YYYY/MM/DD. but actualy i prefer to use DD/MM/YYYY format.

我可以更改列日期格式吗?

推荐答案

否,您不能更改DATE,DATETIME或TIMESTAMP列的默认MySQL格式.

No, you can't change the default MySQL format for DATE, DATETIME or TIMESTAMP columns.

但是您可以在SQL语句中使用MySQL函数,以不同格式的字符串形式输出DATE表达式.

But you can use MySQL functions in your SQL statements to output a DATE expression as a string in different format.

DATE_FORMAT( datecol , '%m/%d/%Y')  AS datecol

(这在SELECT列表中可以很好地工作,但请避免在任何谓词(即WHERE子句)中使用它.在那里,您将要引用裸列,并转换首选格式'MM/DD/YYYY"使用STR_TO_DATE函数,例如

(That will work fine in SELECT list, but avoid using this in any predicates (i.e. the WHERE clause). There, you'll want to reference the bare column, and convert strings of your preferred format 'MM/DD/YYYY' using the STR_TO_DATE function, e.g.

datecol >= STR_TO_DATE('07/16/2012','%m/%d/%Y')


话虽如此,我认为在与数据库的交互中使用MySQL默认的DATE格式并处理代码中的格式更改确实可以为您提供更好的服务.


With that said, I think you will really be better served using the MySQL default DATE format in your interactions with the database, and handling formatting changes in your code.

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

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