常用的日期函数:
--获取系统当前时间 SELECT GETDATE()
--DATEADD()有三个参数,参数1:指定的日期部分,参数2:整数值,参数3:有效的日期格式: SELECT DATEADD(MM,1,GETDATE)
--在当前系统月份加1
SELECT DATEADD(MM,-1,GETDATE)
这一句代表在今天的月份上加一,MM是月分是缩些,后面的是系统的当前日期。
--如果参数2是小数的话,它会直接舍去小数部分取整: SELECT DATEADD(MM,1.5,GETDATE)
--DATEDLFF 获取两个日期之间的差: SELECT DATEDIFF(YY,'2008-8-08',GETDATE)
这句代表的意思是奥运的日期与系统当前日期差了几年,YY代表是是年。
如果开始日期大于结束日期不会报错,会给一个负数:
SELECT DATEDIFF(YY,GETDATE,'2008-8-08')
DATEPAPT与DATENAME的区别:
DATEPAPT(YY,GETDATE())+DATEPAPT(DD,GETDATE) DATENAME(YY,GETDATE())+DATENAME(DD,GETDATE)
DATENAME返回的是日期指定的一个字符形式,+号起的是连接作用,因为+号左右都是字符形式。
DATEPAPT返回到的是日期指定的一个数值形式,+起的相加运算是算术运算符。