MySQL DATE_FORMAT函数
时间:2019-11-20 08:52:22 来源:igfitidea点击:
简介:在本教程中,您将学习如何使用MySQL DATE_FORMAT函数基于特定格式来格式化日期值。
MySQL DATE_FORMAT函数简介
要将日期值格式化为特定格式,请使用DATE_FORMAT函数。
DATE_FORMAT函数的语法如下:
DATE_FORMAT(date,format)
DATE_FORMAT函数接受两个参数:
- date:是您要格式化的有效日期值
- format:是由预定义说明符组成的格式字符串。
每个说明符前面都有一个百分比字符(%)。
有关预定义说明符的列表,请参见下表。
DATE_FORMAT函数返回一个字符串,其字符集和排序规则取决于客户端连接的设置。
下表说明了可用于构造日期格式字符串的说明符及其含义:
Specifier | Meaning |
---|---|
%a | Three-characters abbreviated weekday name e.g., Mon, Tue, Wed, etc. |
%b | Three-characters abbreviated month name e.g., Jan, Feb, Mar, etc. |
%c | Month in numeric e.g., 1, 2, 3…12 |
%D | Day of the month with English suffix e.g., 0th, 1st, 2nd, etc. |
%d | Day of the month with leading zero if it is 1 number e.g., 00, 01,02, …31 |
%e | Day of the month without leading zero e.g., 1,2,…31 |
%f | Microseconds in the range of 000000..999999 |
%H | Hour in 24-hour format with leading zero e.g., 00..23 |
%h | Hour in 12-hour format with leading zero e.g., 01, 02…12 |
%I | Same as %h |
%i | Minutes with leading zero e.g., 00, 01,…59 |
%j | Day of year with leading zero e.g., 001,002,…366 |
%k | Hour in 24-hour format without leading zero e.g., 0,1,2…23 |
%l | Hour in 12-hour format without leading zero e.g., 1,2…12 |
%M | Full month name e.g., January, February,…December |
%m | Month name with leading zero e.g., 00,01,02,…12 |
%p | AM or PM, depending on other time specifiers |
%r | Time in 12-hour format hh:mm:ss AM or PM |
%S | Seconds with leading zero 00,01,…59 |
%s | Same as %S |
%T | Time in 24-hour format hh:mm:ss |
%U | Week number with leading zero when the first day of week is Sunday e.g., 00,01,02…53 |
%u | Week number with leading zero when the first day of week is Monday e.g., 00,01,02…53 |
%V | Same as %U; it is used with %X |
%v | Same as %u; it is used with %x |
%W | Full name of weekday e.g., Sunday, Monday,…, Saturday |
%w | Weekday in number (0=Sunday, 1= Monday,etc.) |
%X | Year for the week in four digits where the first day of the week is Sunday; often used with %V |
%x | Year for the week, where the first day of the week is Monday, four digits; used with %v |
%Y | Four digits year e.g., 2000 and 2001. |
%y | Two digits year e.g., 10,11,and 12. |
%% | Add percentage (%) character to the output |
以下是一些常用的日期格式字符串:
DATE_FORMAT string | Formatted date |
---|---|
%Y-%m-%d | 2013-07-04 |
%e/%c/%Y | 4/7/2013 |
%c/%e/%Y | 7/4/2013 |
%d/%m/%Y | 4/7/2013 |
%m/%d/%Y | 7/4/2013 |
%e/%c/%Y %H:%i | 4/7/2013 11:20 |
%c/%e/%Y %H:%i | 7/4/2013 11:20 |
%d/%m/%Y %H:%i | 4/7/2013 11:20 |
%m/%d/%Y %H:%i | 7/4/2013 11:20 |
%e/%c/%Y %T | 4/7/2013 11:20 |
%c/%e/%Y %T | 7/4/2013 11:20 |
%d/%m/%Y %T | 4/7/2013 11:20 |
%m/%d/%Y %T | 7/4/2013 11:20 |
%a %D %b %Y | Thu 4th Jul 2013 |
%a %D %b %Y %H:%i | Thu 4th Jul 2013 11:20 |
%a %D %b %Y %T | Thu 4th Jul 2013 11:20:05 |
%a %b %e %Y | Thu Jul 4 2013 |
%a %b %e %Y %H:%i | Thu Jul 4 2013 11:20 |
%a %b %e %Y %T | Thu Jul 4 2013 11:20:05 |
%W %D %M %Y | Thursday 4th July 2013 |
%W %D %M %Y %H:%i | Thursday 4th July 2013 11:20 |
%W %D %M %Y %T | Thursday 4th July 2013 11:20:05 |
%l:%i %p %b %e, %Y | 7/4/2013 11:20 |
%M %e, %Y | 4-Jul-13 |
%a, %d %b %Y %T | Thu, 04 Jul 2013 11:20:05 |
MySQL DATE_FORMAT示例
让我们看一下示例数据库中的订单表。
要选择订单数据并格式化日期值,请使用以下语句:
SELECT orderNumber, DATE_FORMAT(orderdate, '%Y-%m-%d') orderDate, DATE_FORMAT(requireddate, '%a %D %b %Y') requireddate, DATE_FORMAT(shippedDate, '%W %D %M %Y') shippedDate FROM orders;
我们根据格式字符串指定的不同日期格式来格式化每个订单的订单日期,所需日期和发货日期。
MySQL DATE_FORMAT与ORDER BY
请参见以下示例:
SELECT orderNumber, DATE_FORMAT(shippeddate, '%W %D %M %Y') shippeddate FROM orders WHERE shippeddate IS NOT NULL ORDER BY shippeddate;
在查询中,我们选择了发货日期不为NULL的所有订单,并按发货日期对订单进行了排序。
但是,订单未正确排序。
原因是我们使用出厂日期作为DATE_FORMAT函数输出的别名,该函数是一个字符串,ORDER BY子句使用别名并根据字符串值而不是日期值对订单进行排序。
要解决此问题,我们必须使用与列名不同的别名。
请参阅以下语句:
SELECT orderNumber, DATE_FORMAT(shippeddate, '%W %D %M %Y') 'Shipped date' FROM orders WHERE shippeddate IS NOT NULL ORDER BY shippeddate;
在本教程中,我们向您展示了如何使用MySQL DATE_FORMAT函数基于指定的格式来格式化日期。