- 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/01
- select convert(varchar(100), GETDATE(), 112) -- 20201201
-
- select convert(varchar(100), GETDATE(), 120) --2020-12-01 15:09:51
- select convert(varchar(100), GETDATE(), 121) --2020-12-01 15:10:03.717
-
-
- select convert(varchar(100), GETDATE(), 111)+' ' + convert(char,getdate(),8)
- --select convert(char,getdate(),8)