date_format(datetime, format)
命令说明:按照format指定的格式,将日期时间格式化成字符串。
参数说明:
datetime:类型为TIMESTAMP或者DATE。
format:格式字符串,格式转换符请参见时间格式转换符。
示例:
SELECT date_format(now(), '%Y-%m-%d %H:%i:%s') as col1, now() as col2;
+---------------------+-----------------------+
| col1 | col2 |
+---------------------+-----------------------+
| 2018-12-08 17:06:21 | 2018-12-08 17:06:21.0 |
SELECT date_format(current_date(), '%Y-%m-%d %H-%i-%s') as col1, current_date() as col2;
+---------------------+-----------------------+
| col1 | col2 |
+---------------------+-----------------------+
| 2018-12-08 00-00-00 | 2018-12-08 |
to_char(datetime, format)
命令说明:按照format指定的格式,将日期时间格式化成字符串。
参数说明:
datetime:类型为TIMESTAMP或者DATE。
format:格式字符串,格式转换符请参见时间格式转换符。
示例:
SELECT to_char(now(), '%Y-%m-%d %H:%i:%s') as col1;
+---------------------+
| col1 |
+---------------------+
| 2018-12-08 17:08:56 |
SELECT to_char(current_date(), '%Y-%m-%d %H:%i:%s') as col1;
+---------------------+
| col1 |
+---------------------+
| 2018-12-08 00:00:00 |
SEC_TO_TIME(seconds)
命令说明:返回秒数对应的时间
参数说明:seconds类型为BIGINT
示例:
SELECT SEC_TO_TIME(11111) as col1;
+----------+
| col1 |
+----------+
| 03:05:11 |
TIME_TO_SEC(time)
命令说明:将时间字段转换为当天的秒数
参数说明:time类型为VARCHAR
示例:
SELECT TIME_TO_SEC('22:23:00') as col1;
+-------+
| col1 |
+-------+
| 80580 |
STR_TO_DATE(str,format)
命令说明:按照format格式,将字符串类型的数据转换为TIMESTAMP类型。
返回值类型:DATE
示例:
SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y') as col1;
+------------+
| col1 |
+------------+
| 2013-05-01 |
TIME(expr)
示例:
SELECT TIME('2003-12-31 01:02:03') as col1;
+----------+
| col1 |
+----------+
| 01:02:03 |
TIMESTAMP(expr)
TIMESTAMP(expr1,expr2)
示例:
SELECT TIMESTAMP('2003-12-31 01:02:03') as col1;
+-----------------------+
| col1 |
+-----------------------+
| 2003-12-31 01:02:03.0 |
SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:01') as col1;
+-----------------------+
| col1 |
+-----------------------+
| 2004-01-01 00:00:01 |
TO_DAYS(date)
命令说明:返回从0年开始的天数。
参数说明:date类型为VARCHAR、DATE或者TIMESTAMP。
示例:
SELECT TO_DAYS('2007-10-07') as col1;
+--------+
| col1 |
+--------+
| 733321 |
SELECT TO_DAYS(curdate()) as col1;
+--------+
| col1 |
+--------+
| 737401 |
在文档使用中是否遇到以下问题
更多建议
匿名提交