MySql 日期函数
在 MySql 中经常会用到日期,关于常用的日期函数,做了以下的总结:
1 . now()
作用; 获取当前的日期
除此之外,获取当前日期的函数还有: current_timestamp(); current_time; localtime(); localtime; localtimestamp(); localtimestamp;但是这些日期函数,与 now() 的效果相同, 为了方便记忆,建议使用 now() 来代替上面的函数。
mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select datediff(now(),'2015-1-1');
+----------------------------+
| datediff(now(),'2015-1-1') |
+----------------------------+
| |
+----------------------------+
row in set mysql> select localtime();
+---------------------+
| localtime() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtime;
+---------------------+
| localtime |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtimestamp;
+---------------------+
| localtimestamp |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtimestamp();
+---------------------+
| localtimestamp() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| -- :: |
+---------------------+
row in set
2. sysdate()
作用: 这个日期函数与 now() 类似,不同的地方是: now() 在执行开始时就得到的确定,而 sysdate() 则是在运行时动态得到 值。
如下所示:
mysql> select now(), sleep(30), now();
+---------------------+-----------+---------------------+
| now() | sleep(30) | now() |
+---------------------+-----------+---------------------+
| 2015-05-13 15:01:49 | 0 | 2015-05-13 15:01:49 |
+---------------------+-----------+---------------------+
1 row in set mysql> select sysdate(), sleep(30), sysdate();
+---------------------+-----------+---------------------+
| sysdate() | sleep(30) | sysdate() |
+---------------------+-----------+---------------------+
| 2015-05-13 15:02:52 | 0 | 2015-05-13 15:03:22 |
+---------------------+-----------+---------------------+
1 row in set
在用 now() 时,在途中 sleep 了 30秒 ,但是 Now() 的结果是相同的, 而在 sysdate() 中 sleep 了 30秒, 结果就不同了,结果是 sleep 30 秒后的值
3. curdate()
作用; 获取当前日期
函数 current_date(); current_date; 与 curdate() 功能一样
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2015-05-13 |
+------------+
1 row in set mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2015-05-13 |
+----------------+
1 row in set mysql> select current_date;
+--------------+
| current_date |
+--------------+
| 2015-05-13 |
+--------------+
1 row in set
4. curtime()
作用: 获取当前时间 ,其功能与 current_time current_time() 是一样的
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 15:04:42 |
+-----------+
1 row in set mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 15:05:05 |
+----------------+
1 row in set mysql> select current_time;
+--------------+
| current_time |
+--------------+
| 15:05:09 |
+--------------+
1 row in set
5. utc_date(); utc_time(); utc_timestamp();
作用: 获取当前 utc 时间的函数
mysql> select utc_timestamp(), utc_date(),utc_time(),now();
+---------------------+------------+------------+---------------------+
| utc_timestamp() | utc_date() | utc_time() | now() |
+---------------------+------------+------------+---------------------+
| 2015-05-13 07:05:51 | 2015-05-13 | 07:05:51 | 2015-05-13 15:05:51 |
+---------------------+------------+------------+---------------------+
1 row in set
6. date(); year(); month(); day(); time(); week(); hour(); minute(); second(); microsecond();
作用; 获取日期中的部分值
mysql> set @date='2015-05-05 14:23:34.345687'
-> select date(@date)
-> select time(@date)
-> select year(@date)
-> select quarter(@date)
-> select month(@date)
-> select week(@date)
-> select day(@date)
-> select hour(@date)
-> select minute(@date)
-> select second(@date)
-> select microsecond(@date);
7. extract()
作用: 和上面所列举的函数功能一样, 只是函数的书写方式不同
mysql> set @date='2015-05-05 14:23:34.345687'
-> select extract(year from @date)
8. last_day()
作用: 返回月份的最后一天
9. datedifff()
作用: 计算两个日期间的相差的天数
mysql> select datediff(now(),'2015-05-05');
+------------------------------+
| datediff(now(),'2015-05-05') |
+------------------------------+
| 8 |
+------------------------------+
10. str_to_date(str,format)
作用: 可以把一些杂乱无章的字符串转换成日期格式,也可以转换成时间
有关更多的 Mysql 函数,可以参考 Mysql 在线手册:http://www.cbi.pku.edu.cn/chinese/documents/csdoc/mysql/manual_toc.html
MySql 日期函数的更多相关文章
- [php基础]Mysql日期函数:日期时间格式转换函数详解
在PHP网站开发中,Mysql数据库设计中日期时间字段必不可少,由于Mysql日期函数输出的日期格式与PHP日期函数之间的日期格式兼容性不够,这就需要根据网站实际情况使用Mysql或PHP日期转换函数 ...
- mysql 日期函数总结
1.0 格式化:DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) date 参数是合法的日期.format 规定日期/时间的 ...
- MYSQL 日期函数【转】
MySQL日期时间函数大全 DAYOFWEEK(date) 返回日期date是星期几(=星期六,ODBC标准) mysql> select DAYOFWEEK('1998-02-03'); WE ...
- mysql日期函数(转)
MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...
- Mysql日期函数,时间函数使用的总结
一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now();+--------------------- ...
- MySQL:日期函数、时间函数总结
MySQL 获得当前日期时间 函数 查询昨天,时间拼接 select concat(DATE_FORMAT(date_add(now(), interval -1 day),'%Y-%d-%d'),& ...
- MySQL日期函数、时间函数总结(MySQL 5.X)
一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...
- mysql 日期函数大全
对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描述见7.3.6 日期和时间类型. 这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内: my ...
- MySQL日期函数与日期转换格式化函数大全
Mysql作为一款开元的免费关系型数据库,用户基础非常庞大,本文列出了MYSQL常用日期函数与日期转换格式化函数 1.DAYOFWEEK(date) 1 2 SELECT DAYOFWEEK('201 ...
随机推荐
- C# 理解lock
本文为转载 .. 一. 为什么要lock,lock了什么? 当我们使用线程的时候,效率最高的方式当然是异步,即各个线程同时运行,其间不相互依赖和等待.但当不同的线程都需要访问某个资源的时候,就需要同步 ...
- 宏定义中的##操作符和... and _ _VA_ARGS_ _
1.Preprocessor Glue: The ## Operator 预处理连接符:##操作符 Like the # operator, the ## operator can be used i ...
- WLS_Oracle Weblogic安装和环境搭建(案例)
2014-01-03 Created By BaoXinjian
- OAF_架构MVC系列1 - MVC的概述(概念)
2015-04-03 Created By BaoXinjian
- BIP_Oracle Erp标准银行接口XML文件(案例)(待整理)
2014-07-07 Created By BaoXinjian
- sublime代码片段功能
tools - > developer - > new snippet有了这个,你就可以通过iu这两个键,直接弄出自己的模板了,这就很方便了,不用在复制粘贴了.上是代码模板,下面是快捷键, ...
- 【jQuery】关于选择器中的 :first 、 :first-child 、 :first-of-type
[:first] <!DOCTYPE html><html lang="zh-CN"><head> <title>test&l ...
- js封装 与 js高级用法 问题集合
1. 什么是自执行的匿名函数? 它是指形如这样的函数: (function {// code})(); 2. 疑问 为什么(function {// code})();可以被执行, 而function ...
- 无法向会话状态服务器发出会话状态请求请。确保 ASP.NET State Service (ASP.NET 状态服务)已启动
原文链接:http://www.cnblogs.com/IT-Bear/archive/2012/01/04/2311546.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET S ...
- Android的所有权限说明
Android权限分的很细,但命名比较人性化,Android permission比SymbianCapabilities有了不少改进,下面就来看看权限许可都有哪些定义吧,发现还是比较繁多的,如果发现 ...