zoukankan      html  css  js  c++  java
  • sql server 中DateName()函数及DatePart()函数

    Datepart()返回代表指定日期的指定日期部分的整数

    语法:Datepart(datepart,date)  返回类型:int

    DateName():返回代表指定日期的指定日期部分的字符串

    语法:DateName(datepart,date返回类型:nvarchar

    日期部分缩写备注
    year yy, yyyy
    quarter qq, q 季度
    month mm, m
    dayofyear dy, y 一年中的第几天
    day dd, d
    week wk, ww 一年中的第几周
    weekday dw日期部分返回对应于星期中的某天的数,例如:Sunday =1 星期几
    Hour hh 小时
    minute mi, n 分钟
    second ss, s
    millisecond ms 毫秒
    复制代码
    select GETDATE() as '当前日期时间',
    DateName(year,GetDate())+'-'+DateName(month,GetDate())+'-'+DateName(day,GetDate()) as '当前日期', 
    DateName(quarter,GetDate()) as '第几季度',
    DateName(week,GetDate()) as '一年中的第几周',
    DateName(DAYOFYEAR,GetDate()) as '一年中的第几天',
    DateName(year,GetDate()) as '年',
    DateName(month,GetDate()) as '月',
    DateName(day,GetDate()) as '日',
    DateName(hour,GetDate()) as '时',
    DateName(minute,GetDate()) as '分',
    DateName(second,GetDate()) as '秒',
    DateName(MILLISECOND,GetDate()) as '豪秒',
    DateName(WEEKDAY,GetDate()) as '星期几'
    
    
    select GETDATE() as '当前日期时间',
    DatePart(year,GetDate())+'-'+DatePart(month,GetDate())+'-'+DatePart(day,GetDate()) as '当前日期', 
    DatePart(quarter,GetDate()) as '第几季度',
    DatePart(week,GetDate()) as '一年中的第几周',
    DatePart(DAYOFYEAR,GetDate()) as '一年中的第几天',
    DatePart(year,GetDate()) as '年',
    DatePart(month,GetDate()) as '月',
    DatePart(day,GetDate()) as '日',
    DatePart(hour,GetDate()) as '时',
    DatePart(minute,GetDate()) as '分',
    DatePart(second,GetDate()) as '秒',
    DatePart(MILLISECOND,GetDate()) as '豪秒',
    DatePart(WEEKDAY,GetDate()) as '星期几'
    复制代码

    查询结果:

    注意:

    1)因为DatePart返回类型为int类型,所以当前日期的结果是做了运算的结果

    2)

    在多数SQL SERVER 英文版本中(以及部分繁体版),

    SELECT DATENAME(month, getdate())  得到 字符串类型的 January ;

    而在简体中文版中:SELECT DATENAME(month, getdate()) 得到  字符串类型的  01 

    而SELECT DATEPART(month,getdate())则在所有版本中都得到  int类型的 1

     3)

    SELECT DATENAME(weekday, getdate()) 得到“星期X”

    SELECT DATEPART(weekday, getdate()) 得到星期对应的数字,一(1)/二(2)/三(3).。。。。

  • 相关阅读:
    Python 模块 itertools
    Python 字符串的encode与decode
    python 模块 hashlib(提供多个不同的加密算法)
    暴力尝试安卓gesture.key
    hdu 1300 Pearls(DP)
    hdu 1232 畅通工程(并查集)
    hdu 1856 More is better(并查集)
    hdu 1198 Farm Irrigation(并查集)
    hdu 3635 Dragon Balls(并查集)
    hdu 3038 How Many Answers Are Wrong(并查集)
  • 原文地址:https://www.cnblogs.com/shiyh/p/9511336.html
Copyright © 2011-2022 走看看