定义和用法
DATEADD() 函数在日期中添加或减去指定的时间间隔
DATEADD(datepart,number,date)
DATEDIFF() 函数返回两个日期之间的时间。(注意startdate,enddate给0的话,是和1900-01-01比较的)
DATEDIFF(datepart,startdate,enddate)
简单尝试
select DATEDIFF(YYYY, 0, GETDATE()) select DATEDIFF(MONTH, 0, GETDATE()) select DATEDIFF(DD, 0, GETDATE()) select DATEADD(yyyy,DATEDIFF(yyyy, 0, GETDATE()),0) select DATEADD(MONTH,DATEDIFF(MONTH, 0, GETDATE()),0) select DATEADD(DD,DATEDIFF(DD, 0, GETDATE()),0)
使用DATEADD() DATEDIFF() 函数获取时间查询条件(这里以今天/明天为例)
select DATEADD(DD,DATEDIFF(DD, 0, GETDATE()),0) select DATEADD(DD,DATEDIFF(DD, 0, GETDATE()),1) select CONVERT(datetime,CONVERT(varchar(50),GETDATE(),23)) select CONVERT(datetime,CONVERT(varchar(50),DATEADD(DD,1,GETDATE()),23)) select CAST(CONVERT(varchar(50),GETDATE(),23) as datetime) select CAST(CONVERT(varchar(50),DATEADD(DD,1,GETDATE()),23) as datetime)