MySQL日期相加减
时间相加:
set @dt = now();
select @dt,'当前时间' from dual
union all
select date_add(@dt, interval 1 day),'加1天' from dual
union all
select date_add(@dt, interval 1 hour),'加1小时' from dual
union all
select date_add(@dt, interval 1 minute),'加1分钟' from dual
union all
select date_add(@dt, interval 1 second),'加1秒' from dual
union all
select date_add(@dt, interval 1 microsecond),'加1毫秒' from dual
union all
select date_add(@dt, interval 1 week),'加1周' from dual--
union all
select date_add(@dt, interval 1 month),'加1月' from dual
union all
select date_add(@dt, interval 1 quarter),'加1季' from dual
union all
select date_add(@dt, interval 1 year),'加1年' from dual
set @dt = curdate();
select @dt,'当前时间' from dual
union all
select date_add(@dt, interval 1 day),'加1天' from dual
union all
select date_add(@dt, interval 1 hour),'加1小时' from dual
union all
select date_add(@dt, interval 1 minute),'加1分钟' from dual
union all
select date_add(@dt, interval 1 second),'加1秒' from dual
union all
select date_add(@dt, interval 1 microsecond),'加1毫秒' from dual
union all
select date_add(@dt, interval 1 week),'加1周' from dual--
union all
select date_add(@dt, interval 1 month),'加1月' from dual
union all
select date_add(@dt, interval 1 quarter),'加1季' from dual
union all
select date_add(@dt, interval 1 year),'加1年' from dual
set @dt = curtime();
select @dt,'当前时间' from dual
union all
select date_add(@dt, interval 1 day),'加1天' from dual
union all
select date_add(@dt, interval 1 hour),'加1小时' from dual
union all
select date_add(@dt, interval 1 minute),'加1分钟' from dual
union all
select date_add(@dt, interval 1 second),'加1秒' from dual
union all
select date_add(@dt, interval 1 microsecond),'加1毫秒' from dual
union all
select date_add(@dt, interval 1 week),'加1周' from dual--
union all
select date_add(@dt, interval 1 month),'加1月' from dual
union all
select date_add(@dt, interval 1 quarter),'加1季' from dual
union all
select date_add(@dt, interval 1 year),'加1年' from dual
说明:
MySQL adddate(), addtime()函数,可以用 date_add() 来替代。
时间相减:
set @dt = now();
select @dt,'当前时间' from dual
union all
select date_sub(@dt, interval 1 day),'减1天' from dual
union all
select date_sub(@dt, interval 1 hour),'减1小时' from dual
union all
select date_sub(@dt, interval 1 minute),'减1分钟' from dual
union all
select date_sub(@dt, interval 1 second),'减1秒' from dual
union all
select date_sub(@dt, interval 1 microsecond),'减1毫秒' from dual
union all
select date_sub(@dt, interval 1 week),'减1周' from dual--
union all
select date_sub(@dt, interval 1 month),'减1月' from dual
union all
select date_sub(@dt, interval 1 quarter),'减1季' from dual
union all
select date_sub(@dt, interval 1 year),'减1年' from dual
说明:
与相加类似,不在进行复述。
MySQL中还有两个函数subdate(),subtime(),建议用date_sub()来替代。
另类日期函数:period_add(P,N), period_diff(P1,P2)
说明:函数参数“P”的格式为“YYYYMM”或者“YYMM”,第二个参数“N”表示增加或减去 N month(月)。
select period_add(200808,2), period_add(20080808,-2) from dual
select period_diff(200808, 200801),period_diff(20080801, 20080802);
MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)
MySQL datediff(date1,date2):
两个日期相减 date1 date2,返回天数。
select datediff('2008-08-08', '2008-08-01')
union all
select datediff('2008-08-01', '2008-08-08');
MySQL timediff(time1,time2):
两个日期相减 time1 time2,返回 time 差值。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00')
union all
select timediff('08:08:08', '00:00:00');
注意:timediff(time1,time2) 函数的两个参数类型必须相同。
DATE ( date ); 提取日期或日期/时间表达式的日期部分
select now() from dual
union all
select date(now()) from dual;
select date(now()) from dual;