​    SQL SERVER 中相关的时间    ​​        


1、获取当前时间 

select getdate()2、截取需要的值select datepart(year,getdate())select datepart(month,getdate())select datepart(day,getdate())select datepart(hour,getdate())select datepart(minute,getdate())select datepart(second,getdate())select datepart(week,getdate())3、在日期中添加或减去指定的时间间隔select dateadd(year,3,getdate()) --获取当前时间,往后推迟三年select dateadd(month,3,getdate()) --获取当前时间,往后推迟三个月select dateadd(day,3,getdate()) --获取当前时间,往后推迟三天select dateadd(hour,3,getdate()) --获取当前时间,往后推迟三小时select dateadd(minute,3,getdate()) --获取当前时间,往后推迟三分钟select dateadd(second,3,getdate()) --获取当前时间,往后推迟三秒钟4、返回两个日期之间的时间select datediff(year,'2020/11/30',getdate()) --2001-08-19和当前时间之间差多少年select datediff(month,'2020/11/30',getdate()) --2001-08-19和当前时间之间差多少月select datediff(day,'2020/11/30',getdate()) --2001-08-19和当前时间之间差多少天5、用不同的格式显示日期/时间select convert(char,getdate(),8) --显示当前时:分:秒 "15:00:19"select convert(char,getdate(),10) --显示当前月-日-年,显示形式“12-01-20”select convert(char,getdate(),11) --显示当前年-月-日,显示形式“20/12/01”select convert(char,getdate(),14) --显示当前时-分-秒-毫秒,显示形式“14:58:06:340"--------select convert(varchar(100), GETDATE(), 111) -- 2020/12/01select convert(varchar(100), GETDATE(), 112) -- 20201201select convert(varchar(100), GETDATE(), 120) --2020-12-01 15:09:51select convert(varchar(100), GETDATE(), 121) --2020-12-01 15:10:03.717select convert(varchar(100), GETDATE(), 111)+' ' + convert(char,getdate(),8)--select convert(char,getdate(),8)