加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

关于SqlServer数据库日期函数

发布时间:2020-12-12 14:12:09 所属栏目:MsSql教程 来源:网络整理
导读:关于SqlServer数据库日期函数 1、获取当前时间 select getdate() 2、截取需要的值 select datepart(year,getdate()) select datepart(month,sans-serif; font-size:13px">select datepart(day,sans-serif; font-size:13px">select datepart(hour,sans-serif;

1、获取当前时间 select getdate()

2、截取需要的值
select datepart(year,getdate())
select datepart(month,sans-serif; font-size:13px">select datepart(day,sans-serif; font-size:13px">select datepart(hour,sans-serif; font-size:13px">select datepart(minute,sans-serif; font-size:13px">select datepart(second,sans-serif; font-size:13px">select datepart(week,sans-serif; font-size:13px">3、在日期中添加或减去指定的时间间隔
select dateadd(year,3,getdate()) --获取当前时间,往后推迟三年
select dateadd(month,getdate()) --获取当前时间,往后推迟三个月
select dateadd(day,getdate()) --获取当前时间,往后推迟三天
select dateadd(hour,getdate()) --获取当前时间,往后推迟三小时
select dateadd(minute,getdate()) --获取当前时间,往后推迟三分钟
select dateadd(second,getdate()) --获取当前时间,往后推迟三秒钟
4、返回两个日期之间的时间
select datediff(year,'2001-08-19',getdate()) --2001-08-19和当前时间之间差多少年
select datediff(month,getdate()) --2001-08-19和当前时间之间差多少月
select datediff(day,getdate()) --2001-08-19和当前时间之间差多少天
5、用不同的格式显示日期/时间
select convert(char,getdate(),8)? --显示当前时-分-秒
注:参数1-14随意。

?使用sqlserver日期函数获取当前日期?

使用sqlserver日期函数获取当前日期和时间,通过使用convert(varchar(10),120)的方法可以得到当前的日期,不需要时间部分,或者不需要日期只要时间部分。

使用 sqlserver日期函数中的getdate()可以获取当现的日期,下面就将为您介绍这种使用sqlserver日期函数获取当前日期的方法,供您参考,希望对您学习sqlserver日期函数有所启迪。 ? 但是如果我们只需要得到当前的日期,不需要时间部分,或者不需要日期只要时间部分,再或者我要只要字段中的日期以某种形式显示,应该怎么操作呢? ? ? 可以使用convert(varchar(10),120)这样的方法来实现,其中varchar(10)定义的是你要的字段的长度,当然长度的不同返回的也会不的,如果我们只要日期部分,设成10正好为日期长度,如果设成19则正好可以读到时间部分。 现在介绍最后面代的参数,分别以代的参数及实例效果说明 ? 参数 结果 100 05 8 2006 9:27PM 101 05/08/2006 102 2006.05.08 103 08/05/2006 104 08.05.2006 105 08-05-2006 106 08 05 2006 107 05 08,2006 108 21:30:51 109 05 8 2006 9:31:11 110 05-08-2006 111 2006/05/08 112 20060508 113 08 05 2006 21:31:59 114 21:33:06:503 120 2006-05-08 21:33:38

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

关于SqlServer数据库日期函数

    推荐文章
      热点阅读