mysql日期函数学习


/*系统时间可以用 now() 或  sysdate()   */
/*
update myact.lsy_test 
	set startdate = now()
	where id = '1111' ;
*/

/*
update myact.lsy_test 
	set startdate = sysdate()
	where id = '1112' ;

*/

/*
select now(),sysdate() from dual;

*/


/*  year() 返回年  */
/*
select now(),year(now()),year('2013-04-17 20:25:01') 
,year('2013-04-17 20:25') ,year('2013-04-17 20')
,year('2013-04-17') 
from dual;
*/
now()year(now())year('2013-04-17 20:25:01')year('2013-04-17 20:25')year('2013-04-17 20')year('2013-04-17')
2013-04-19 15:12:3820132013201320132013


/*  month() 返回月份  */
/*
select now(),month(now()),month('2013-04-17 20:25:01') 
,month('2013-04-17 20:25') ,month('2013-04-17 20')
,month('2013-04-17') 
from dual;
*/

now()month(now())month('2013-04-17 20:25:01')month('2013-04-17 20:25')month('2013-04-17 20')month('2013-04-17')
2013-04-19 15:19:2144444

/*  dayofmonth() 返回日  */
/*
select now(),dayofmonth(now()),dayofmonth('2013-04-17 20:25:01') 
,dayofmonth('2013-04-17 20:25') ,dayofmonth('2013-04-17 20')
,dayofmonth('2013-04-17') 
from dual;
*/

now()dayofmonth(now())dayofmonth('2013-04-17 20:25:01')dayofmonth('2013-04-17 20:25')dayofmonth('2013-04-17 20')dayofmonth('2013-04-17')
2013-04-19 15:20:451917171717


/*  hour() 返回小时  */
/*
select now(),hour(now()),hour('2013-04-17 20:25:01') 
,hour('2013-04-17 20:25') ,hour('2013-04-17 20')
from dual;
*/

now()hour(now())hour('2013-04-17 20:25:01')hour('2013-04-17 20:25')hour('2013-04-17 20')
2013-04-19 15:21:4215202020

/*  minute() 返回分钟  */
/*
select now(),minute(now()),minute('2013-04-17 20:25:01') 
,minute('2013-04-17 20:25') 
from dual;
*/
now()minute(now())minute('2013-04-17 20:25:01')minute('2013-04-17 20:25')
2013-04-19 15:22:25222525

/*  second() 返回秒  */
/*
select now(),second(now()),second('2013-04-17 20:25:01') 
from dual;
*/

now()second(now())second('2013-04-17 20:25:01')
2013-04-19 15:23:14141


/*  weekday() 返回星期几  */
/*
select now(),weekday(now()),weekday('2013-04-17 20:25:01') 
,weekday('2013-04-17 20:25') ,weekday('2013-04-17 20')
,weekday('2013-04-17')  
from dual;
*/

now()weekday(now())weekday('2013-04-17 20:25:01')weekday('2013-04-17 20:25')weekday('2013-04-17 20')weekday('2013-04-17')
2013-04-19 15:24:0842222




不常用的方法。
dayofyear(),返回日期在一年中的第几天。
dayname(),返回日期是星期几(完整英文单词)。
monthname(),返回日期是几月(完整英文单词)。
quarter(),返回日期是第几季度(1-4)
week(),返回日期是第几周,(1-52)
 

select now(),dayofyear(now()),
dayname(now()),monthname(now()),
quarter(now()),week(now())
from dual;

now()dayofyear(now())dayname(now())monthname(now())quarter(now())week(now())
2013-04-19 15:25:15109FridayApril215

上面的方法,除了可以接受date类型参数,还可以接受字符串类型的日期参数,如下:

select now(),dayofyear('2013-04-17 20:53:46'),
dayname('2013-04-17 20:53:46'),monthname('2013-04-17 20:53:46'),
quarter('2013-04-17 20:53:46'),week('2013-04-17 20:53:46')
from dual;

now()dayofyear('2013-04-17 20:53:46')dayname('2013-04-17 20:53:46')monthname('2013-04-17 20:53:46')quarter('2013-04-17 20:53:46')week('2013-04-17 20:53:46')
2013-04-19 15:25:56107WednesdayApril215



/*  下面复习一下,mysql字符串与日期的相互转化 */


/* 一,字符串转日期 str_to_date(exp1,exp2) */

select str_to_date('2013-04-17 21:00:11','%Y-%m-%d %H:%i:%s') 
, str_to_date('2013-04-17','%Y-%m-%d') ,str_to_date('21:00:11','%H:%i:%s') 
,str_to_date('2013年04月17日','%Y年%m月%d日') ,str_to_date('2013年04月17日 21点00分11秒','%Y年%m月%d日 %H点%i分%s秒') 
 from dual; 

str_to_date('2013-04-17 21:00:11','%Y-%m-%d %H:%i:%s')str_to_date('2013-04-17','%Y-%m-%d')str_to_date('21:00:11','%H:%i:%s')str_to_date('2013年04月17日','%Y年%m月%d日')str_to_date('2013年04月17日 21点00分11秒','%Y年%m月%d日 %H点%i分%s秒')
2013-04-17 21:00:112013-04-1721:00:112013-04-172013-04-17 21:00:11

/*
字符串转日期,exp1需要和exp2要匹配,最后才能转化成功,否则结果null。
str_to_date还可以对日期进行格式化,第一个参数还可以是date。
注意,格式化参数里如果没有定义的参数,就显示0.
例如,str_to_date(now(),'%Y'),月日没有定义,所以就过是2013-00-00

*/


/*  二,日期转字符串 date_format(exp1,exp2)   */

select date_format(now(),'%Y-%m-%d %H:%i:%s')
, date_format(now(),'%Y-%m-%d %H:%i')
, date_format(now(),'%Y-%m-%d %H')
, date_format(now(),'%Y-%m-%d')
from dual;

date_format(now(),'%Y-%m-%d %H:%i:%s')date_format(now(),'%Y-%m-%d %H:%i')date_format(now(),'%Y-%m-%d %H')date_format(now(),'%Y-%m-%d')
2013-04-19 15:29:162013-04-19 15:292013-04-19 152013-04-19



select date_format(now(),'%Y-%m-%d %H:%i:%s')
, date_format('2013-04-19 14:19:58','%Y-%m-%d %H:%i:%s')
, date_format('2013-04-19 14:19:58','%Y-%m-%d %H:%i')
, date_format('2013-04-19 14:19:58','%Y-%m-%d %H')
, date_format('2013-04-19 14:19:58','%Y-%m-%d')
from dual;

date_format(now(),'%Y-%m-%d %H:%i:%s')date_format('2013-04-19 14:19:58','%Y-%m-%d %H:%i:%s')date_format('2013-04-19 14:19:58','%Y-%m-%d %H:%i')date_format('2013-04-19 14:19:58','%Y-%m-%d %H')date_format('2013-04-19 14:19:58','%Y-%m-%d')
2013-04-19 15:30:032013-04-19 14:19:582013-04-19 14:192013-04-19 142013-04-19


select date_format(now(),'%Y-%m-%d %H:%i:%s')
, date_format('2013-04-19','%Y-%m-%d %H:%i:%s')
, date_format('2013-04-19','%Y-%m-%d %H:%i')
, date_format('2013-04-19','%Y-%m-%d %H')
, date_format('2013-04-19','%Y-%m-%d')
from dual;

date_format(now(),'%Y-%m-%d %H:%i:%s')date_format('2013-04-19','%Y-%m-%d %H:%i:%s')date_format('2013-04-19','%Y-%m-%d %H:%i')date_format('2013-04-19','%Y-%m-%d %H')date_format('2013-04-19','%Y-%m-%d')
2013-04-19 15:30:302013-04-19 00:00:002013-04-19 00:002013-04-19 002013-04-19


/*
无论str_to_date 还是 date_format ,第一个参数既可以是字符串,也可以是Date类型。
*/


/*
格式	描述
%a	缩写星期名(Sun……Sat)
%b	缩写月名(Jan……Dec)
%c	月,数值(1……12)
%D	带有英文前缀的月中的天(1st, 2nd, 3rd,...)
%d	月的天,数值(00-31)
%e	月的天,数值(0-31)
%f	微秒
%H	小时 (00-23)
%h	小时 (01-12)
%I	小时 (01-12)
%i	分钟,数值(00-59)
%j	年的天 (001-366)
%k	小时 (0-23)
%l	小时 (1-12)
%M	月名(January……December) 
%m	月,数值(01-12)
%p	AM 或 PM
%r	时间,12-小时(hh:mm:ss AM 或 PM)
%S	秒(00-59)
%s	秒(00-59)
%T	时间, 24-小时 (hh:mm:ss)
%U	周 (00-53) 星期日是一周的第一天
%u	周 (00-53) 星期一是一周的第一天
%V	周 (01-53) 星期日是一周的第一天,与 %X 使用
%v	周 (01-53) 星期一是一周的第一天,与 %x 使用
%W	星期名(Sunday……Saturday)
%w	周的天 (0=星期日, 6=星期六)
%X	年,其中的星期日是周的第一天,4 位,与 %V 使用
%x	年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y	年,4 位
%y	年,2 位
*/

select now(),date_format(now(),'年:%Y,%y; 月:%m,%c,%M,%b; 日:%p,%d,%e,%D; 时: %H,%h,%I,%l,%k;  分:%i; 秒:%s,%S 星期:%a,%w,%W; 年中天:%j ;时间:%T,%r; 第几周:%U,%u')

now()date_format(now(),'年:%Y,%y; 月:%m,%c,%M,%b; 日:%p,%d,%e,%D; 时: %H,%h,%I,%l,%k; 分:%i; 秒:%s,%S 星期:%a,%w,%W; 年中天:%j ;时间:%T,%r; 第几周:%U,%u')
2013-04-19 15:31:29年:2013,13; 月:04,4,April,Apr; 日:PM,19,19,19th; 时: 15,03,03,3,15; 分:31; 秒:29,29 星期:Fri,5,Friday; 年中天:109 ;时间:15:31:29,03:31:29 PM; 第几周:15,16

或字符串参数

select now(),date_format('2013-04-19 15:32:59','年:%Y,%y; 月:%m,%c,%M,%b; 日:%p,%d,%e,%D; 时: %H,%h,%I,%l,%k;  分:%i; 秒:%s,%S 星期:%a,%w,%W; 年中天:%j ;时间:%T,%r; 第几周:%U,%u')

now()date_format('2013-04-19 15:32:59','年:%Y,%y; 月:%m,%c,%M,%b; 日:%p,%d,%e,%D; 时: %H,%h,%I,%l,%k; 分:%i; 秒:%s,%S 星期:%a,%w,%W; 年中天:%j ;时间:%T,%r; 第几周:%U,%u')
2013-04-19 15:33:19年:2013,13; 月:04,4,April,Apr; 日:PM,19,19,19th; 时: 15,03,03,3,15; 分:32; 秒:59,59 星期:Fri,5,Friday; 年中天:109 ;时间:15:32:59,03:32:59 PM; 第几周:15,16






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值