/*函数 getetime() 获取当前日期和时间*/
select getdate() as 当前时间 from Students_information
/*函数datepart()返回时间的单独部分*/
select datepart(mm,'2018-02-01') from Students_information /*返回的是月份2*/
select datepart(YY,getdate())from Students_information /*返回的是年份*/
/* yy:取年 mm:取月 dd:取天 hh:取小时 ss:取秒 wk:取年中的周 dy:取周中的天 qq:取年中的季度 dy:取年中的天 mi:取分钟 */
/*函数dateadd() 在日期中添加或减去指定的时间间隔*/
/*dd是增加或减去多少天 yy是增加或减去多少年 hh是增加或减去多少小时*/
select dateadd(dd,30,getdate()) from Students_information /*当前的时间增加30天*/
select DATEADD(yy,1,'2018-01-01')from Students_information /*当前的时间增加1年*/
select dateadd(hh,-10,getdate()) from Students_information /*当前的时间减去10小时*/
select * from Students_information
select name from Students_information where admissiondate<dateadd(mm,-10,getdate()) /*查询学生表中入学时间早于当前
时间10个月的学生姓名*/
/*函数datediff() 返回两个日期之间的时间*/
select datediff(dd,getdate(),'2018-01-01') /*查询当前时间与2018-01-01之间的天数*/
SQL日期函数
最新推荐文章于 2024-06-25 17:18:28 发布