Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

sql - changing format date MySql

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.

Could i change the column date format?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

(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')

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...