Mysql's DATE_FORMAT()

Mysql's DATE_FORMAT () application (2011-04-29 17:27:58) reprint ▼
label: mysql java time format it Category: Programming
Today, a classmate who is doing ssh asked the time question and wants to display the time
to I used to use the display to the day before, so I checked it out. I

encountered a MYSQL problem. The content of the field in the table is "year-month-day hour: minute: second", you need to query to match "year month day" or The data entry of "hour:minute:second" can be realized by the following SQL statement at this time:

select * from _table where date_format(_datetime,'%Y%m%d')= '20080227' (matching "year month day" ")
select * from _table where date_format(_datetime,'%H:%i:%s')= '16:40:01' (matching "hour:minute:second")

DATE_FORMAT (date, format) can be based on the format string format formats a date or a date and time value date and returns the result string. DATE or DATETIME values ​​can be formatted with DATE_FORMAT( ) to obtain the desired format. Format the date value according to the format string:

%S, %s Two-digit seconds ( 00,01, . . ., 59)
%i Two-digit minutes ( 00,01, . . ., 59)
%H Two-digit hour, 24 hours (00,01, . . ., 23)
%h, %I Two-digit hour, 12 hours (01,02, . . ., 12)
%k Numerical hour, 24 hours (0,1, . . ., 23)
%l Numerical hour, 12 hours (1, 2, . . ., 12)
%T Time of 24 hours (hh: mm : ss)
%r 12-hour time form (hh:mm:ss AM or hh:mm:ss PM)
%p AM or PM
%W name of the day of the week (Sunday, Monday, . . ., Saturday )
%a Abbreviated name of the day of the week ( Sun, Mon, . . ., Sat)
%d Two-digit day of the month ( 00, 01, . . ., 31)
%e Numeric form of the month Days ( 1, 2, . . ., 31)
%D English suffix for days of the month ( 1st, 2nd, 3rd, . . .)
%w Numerical days of the week ( 0 = Sunday, 1= Monday, . . ., 6=Saturday)
%j Day of the year as three digits ( 001, 002, . . ., 366)
% U Week (0, 1, 52), where Sunday is the day of the week Day
%u Week (0, 1, 52), where Monday is the first day of the week
%M Month name (January, February, . . ., December)
%b Abbreviated month name (January, February, . . ., December)
%m Two-digit month ( 01, 02, . . ., 12)
%c Two-digit month ( 1, 2, . . ., 12)
%Y Four-digit year
%y Two-digit year The year
%% is directly valued as "%". By the


way, the date in mysql seems to be stored only to days, while datetime will only be stored to seconds. The string
that the classmate has always used to store the time, although it can be done, whether it is access or comparison Size, it feels like it will take an extra step
. More importantly, if varchar is used, it is estimated that date_format() should be used. This can only be used with date and datetime.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646748&siteId=291194637