1.日期和时间间字段
date:日期字段,格式:2016-04-15
timestamp:时间字段,格式:2016-04-15 20:00:00
2.获取当时时间函数
select now();
select current_timestamp;
select CURRENT_TIME;
select LOCALTIME;
select LOCALTIMESTAMP;
3.获取当天日期
select current_date;
4.日期计算:
--使用interval
当前时间加2天
select now()+interval '2 day';
当前时间减2天
select now()-interval '2 day';
当前时间减1个月
select now()-interval '-2 mon';
当前时间减2小时
select now()+interval '2 hour';
5.时间截取
显示年
select extract(year from now());
显示月
select extract(mon from now());
6.时间转换
显示年月日时分秒
select timestamp '2012-05-12 18:54:54';
显示年月日
select date '2012-05-12 18:54:54';
显示年月日时分秒+毫秒
select TIMESTAMP WITH TIME ZONE '2012-05-12 18:54:54' ;
7.与unix时间戳的转换
SELECT TIMESTAMP 'epoch' + 1341174767 * INTERVAL '1 second';
本文介绍了SQL中关于日期和时间的操作方法,包括日期时间字段格式、获取当前时间、日期计算、时间截取、时间转换及与Unix时间戳的转换等功能。
1889

被折叠的 条评论
为什么被折叠?



