zoukankan      html  css  js  c++  java
  • Sqlserver日期操作

    Sqlserver日期操作

     select GETDATE() as '当前日期',
    DateName(year,GetDate()) as '年',
    DateName(month,GetDate()) as '月',
    DateName(day,GetDate()) as '日',
    DateName(dw,GetDate()) as '星期',
    DateName(week,GetDate()) as '周数',
    DateName(hour,GetDate()) as '时',
    DateName(minute,GetDate()) as '分',
    DateName(second,GetDate()) as '秒'

    结果:
    2009-08-13 23:07:15.403 2009 08 13 星期四 33 23 7 15

    函数 参数/功能
    GetDate( ) 返回系统目前的日期与时间
    DateDiff (interval,date1,date2) 以interval 指定的方式,返回date2 与date1两个日期之间的差值 date2-date1
    DateAdd (interval,number,date) 以interval指定的方式,加上number之后的日期
    DatePart (interval,date) 返回日期date中,interval指定部分所对应的整数值
    DateName (interval,date) 返回日期date中,interval指定部分所对应的字符串名称


    参数 interval的设定值如下:
    值 缩 写(Sql Server) 说明
    Year Yy 年 1753 ~ 9999
    Quarter Qq 季 1 ~ 4
    Month Mm 月1 ~ 12
    Day of year Dy 一年的日数,一年中的第几日 1-366
    Day Dd 日,1-31
    Weekday Dw 一周的日数,一周中的第几日 1-7
    Week Wk 周,一年中的第几周 0 ~ 51
    Hour Hh 时0 ~ 23
    Minute Mi 分钟0 ~ 59
    Second Ss 秒 0 ~ 59
    Millisecond Ms 毫秒 0 ~ 999

    举例:
    1.GetDate() 用于sql server :select GetDate()

    2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值为 514592 秒
      DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值为 5 天

    3.DatePart('w','2005-7-25 22:56:32')返回值为 2 即星期一(周日为1,周六为7)
      DatePart('d','2005-7-25 22:56:32')返回值为 25即25号
      DatePart('y','2005-7-25 22:56:32')返回值为 206即这一年中第206天
      DatePart('yyyy','2005-7-25 22:56:32')返回值为 2005即2005年

  • 相关阅读:
    java枚举enum
    冒泡排序、选择排序、插入排序、二分法排序、快速排序、二叉树排序、堆排序总结
    Django-tinymce富文本的使用
    Redis-基本操作总结
    git-总结大全
    css-总结
    html-table布局
    html表单示例
    html总结
    python-浅拷贝、深拷贝实例以及讲解
  • 原文地址:https://www.cnblogs.com/xiaohuihui123/p/5404266.html
Copyright © 2011-2022 走看看