SqlServer通过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).。。。。

原文地址:https://www.cnblogs.com/luna-hehe/p/8191873.html

 
原文地址:https://www.cnblogs.com/zyadmin/p/11436272.html