1 adddate(date,interval expr unit)--同 date_add()
select date_add('2019-4-20',interval 31 day); select adddate('2019-4-20',interval 31 day);
相关的函数:subdate()同date_sub()
Adddate(expr,days)---Mysql 把days 作为整数 天 加到 expr 上 select adddate('2019-4-20',31);
2 addtime(expr1,expr2) 返回 添加 expr2到expr1上的结果,expr1是一个时间或一个日期 表达式,expr2是一个时间表达式
select addtime('2019-4-20 23:59:59.999999','1 1:1:1.000002');
select addtime('01:00:00.999999','02:00:00.999998');
3 convert_tz(dt,from_tz,to_tz) 时区转化,如果参数无效返回null
4 curdate() 返回当前日期 返回格式:YYYY-MM-DD 或 YYYYMMDD 基于 这个函数用于字符还是数字上下文环境中
select curdate(); select curdate()+0;
current_date,current_date() 同 curdate()
5 curtime([fsp]) 基于上下文环境-返回当前时间 格式:HH:MM:SS 或 HHMMSS
fsp 指定秒 小数位 0-6位
select curtime(); select curtime(2)+0;
6 date(expr) 提取日期部分 select date(expr);
7 datediff(expr1,expr2) 返回 expr1-expr2 天数,参数可以是日期,也可以是日期+时间,但是只计算日期部分
8 date_add(date,interval expr unit); date_sub(date,interval expr unit);
date:指定开始日期
expr:指定要从开始日期 加或减去的数值,被当作字符类型,可以是负数。
unit:是一个关键字 指定 要加或减去的是:年 月 小时、分、秒
返回值 -如果 date 是 Date值只有年月日,则返回 Date类型,如果 第一个参数是 datetime 或 timestamp 值,或者 第一个参数是日期date unit值是小时、分、秒。其他类型则返回字符串
select date_add('2019-04-23',interval 1 day);
select date_sub('2019-4-23',interval 1 year);
9 date_format(date,format) :根据format格式化日期 format内使用说明字符 %是必须的。
select date_format('2019-04-23 22:59:59:00','%W %M %Y');
select date_format('2019-04-23','%d');
10 dayname(date):返回日期在星期几名称 ,名称的语言取决于 lc_time_names 系统变量
select dayname('2019-04-23');
11 Day(date) ; Dayofmonth(date);返回月份中的天 范围:1-31,如果 0000-00-00 或 2019-00-00 天中包含0时 返回 0
12 dayofweek(date):返回天在一周中的序号(1=sunday).select dayofweek('2019-04-23')
13 dayofyear(date) 返回天在一年中序号,范围:1-366
select dayofyear('2019-04-23');
14 extract(unit from date):从date中提取一部分 unit 使用与date_add() 相同的说明符。select extract(year from '2019-04-23');
select extract(year_month from '2019-04-23'); select extract(day_minute from '2019-04-23 01:59:59');
select extract(microsecond from '2019-04-23 10:30:00.000123');
15 from_days(N):从给定的数字天数,返回 一个日期值 select from_days(100000);
16 from_unixtime(unix_timestamp[,format]):返回给定时间戳对应的日期。返类型取决于上下文。
select from_unixtime(100000,'%Y'); select from_unixtime(100000)+0;
17get_format({date|time|datetime|timestamp},{'eur'|'usa'|'jis'|'iso'|'internal'}) 返回一个格式字符串 在date_format() str_to_date() 函数中很有用
select get-format(datetime,'usa');
select date_format('2019-4-23',get_format(date,'eur'));
18 hour(time):从time返回小时 范围:0-23 。如果小时大于23,返回大于23的小时部分
select hour('272:59:59');
19 last_day(date) 从 date 或 datetime的月份中按日期返回相对应的月的最后一天。如果参数不是有效的,则返回null
select last_day('2019-4-23');
20 localtime,localtime([fsp]) 同 now();取本地当前时间
localtimestamp,localtimestamp([fsp]) 同 now() 取本地当前时间
now([fsp]): 返回当前天和时间,根据上下文 返回格式;yyyy-MM-DD HH:MM:SS 或 YYYYMMDDHHMMSS
select now(); select now()+0;
now()返回语句开始执行时间,sysdate() 返回确切的执行时间 select now(),sleep(2),now(); select sysdate(),sleep(2),sysdate();
21 makedate(year,dayofyear):返回 指定年 和 年中天数 对应的日期,dayofyear必须大于0.否则返回null
select makedate(2019,40);select makedate(2019,365);
22 maketime(hour,minute,second):根据参数 返回对应的时间
select maketime(12,15,30);
23 microsecond(expr):从时间或日期 表达式中-返回毫秒 范围:0-999999
select microsecond('12:00:00.123456');
24 minute(time):从时间参数中返回分钟 ,范围:0-59 select minute('2019-04-24 10:05:03');
25 month(date):从日期中返回对应的月份 范围:1-12 或 0 0000-00-00 select month('2019-4-23');
26 period_add(p,n):在时间段P上加上N个月,P的格式 :YYMM 或 YYYYMM 不是一个日期值.返回形式:YYYYMM
select period_add(201904,2);
27 period_diff(p1,p2): 返回p1和p2之间的月数,p1/p2 必须是YYMM 或YYYYMM 格式,且不是日期值
select period_diff(200802,200703);
28 quarter(date):返回日期参数中的季度 。select quarter('2019-04-24');
29 second(time):返回时间参数的秒,范围:0-59。select second('10:05:03');
30 sec_to_time(seconds):从秒参数返回 小时 分 秒 时间值。 select sec_to_time(2378);select sec_to_time(2378)+0;
31 str_to_date(str,format):与 date_format()相反的函数,参数str format是字符串类型,
如果format包含日期和时间-则返回datetime值,如果只有日期或时间,则只返加日期date 或时间time值
如果从str中返的日期、时间是不合法的,则返回null 或产生一个警告
select str_to_date('01,5,2019','%d,%m,%Y'); select str_to_date('May 1,2019','%M %d,%Y');
从str的开头对比,如果 匹配不到format,匹配失败。在str结尾多余的字符被忽略
select str_to_date('a09:30:17','a%h:%i:%s'); select str_to_date('a09:30:17','%h:%i:%s');
select str_to_date('09:30:17a','%h:%i:%s');
没有指定的部分被赋值为0 set sql_mode=''; select str_to_date('9','%m');
如果 sql_mode=no_zero_date 或 no_zero_in_date,0日期或部分为0是不允许的,返回null 或 警告