经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
SQL SERVER 中相关的时间
来源:cnblogs  作者:農碼一生  时间:2020/12/8 9:01:35  对本文有异议

SQL SERVER 中相关的时间

  1. 1、获取当前时间
  2. select getdate()
  3. 2、截取需要的值
  4. select datepart(year,getdate())
  5. select datepart(month,getdate())
  6. select datepart(day,getdate())
  7. select datepart(hour,getdate())
  8. select datepart(minute,getdate())
  9. select datepart(second,getdate())
  10. select datepart(week,getdate())
  11. 3、在日期中添加或减去指定的时间间隔
  12. select dateadd(year,3,getdate()) --获取当前时间,往后推迟三年
  13.  
  14. select dateadd(month,3,getdate()) --获取当前时间,往后推迟三个月
  15.  
  16. select dateadd(day,3,getdate()) --获取当前时间,往后推迟三天
  17.  
  18. select dateadd(hour,3,getdate()) --获取当前时间,往后推迟三小时
  19.  
  20. select dateadd(minute,3,getdate()) --获取当前时间,往后推迟三分钟
  21.  
  22. select dateadd(second,3,getdate()) --获取当前时间,往后推迟三秒钟
  23.  
  24. 4、返回两个日期之间的时间
  25. select datediff(year,'2020/11/30',getdate()) --2001-08-19和当前时间之间差多少年
  26.  
  27. select datediff(month,'2020/11/30',getdate()) --2001-08-19和当前时间之间差多少月
  28.  
  29. select datediff(day,'2020/11/30',getdate()) --2001-08-19和当前时间之间差多少天
  30.  
  31. 5、用不同的格式显示日期/时间
  32. select convert(char,getdate(),8) --显示当前时:分:秒 "15:00:19"
  33.  
  34. select convert(char,getdate(),10) --显示当前月-日-年,显示形式“12-01-20
  35.  
  36. select convert(char,getdate(),11) --显示当前年-月-日,显示形式“20/12/01
  37.  
  38.  
  39. select convert(char,getdate(),14) --显示当前时-分-秒-毫秒,显示形式“14:58:06:340"
  40.  
  41. --------
  42.  
  43. select convert(varchar(100), GETDATE(), 111) -- 2020/12/01
  44. select convert(varchar(100), GETDATE(), 112) -- 20201201
  45.  
  46. select convert(varchar(100), GETDATE(), 120) --2020-12-01 15:09:51
  47. select convert(varchar(100), GETDATE(), 121) --2020-12-01 15:10:03.717
  48.  
  49.  
  50. select convert(varchar(100), GETDATE(), 111)+' ' + convert(char,getdate(),8)
  51. --select convert(char,getdate(),8)

 

原文链接:http://www.cnblogs.com/wml-it/p/14075535.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号