datediff结合cast函数求出两日期之间的年差、月差、日差

declare @Birthday varchar(10)
set @Birthday = '2010-10-16'
select abs(datediff(yy,getdate(),cast(@Birthday as datetime)))
select abs(datediff(mm,getdate(),cast(@Birthday as datetime)))

select abs(datediff(dd,getdate(),cast(@Birthday as datetime)))


SELECT DATEADD(day, -1, getdate())  ---求当前日期前一天
SELECT DATEADD(week,-1, getdate())  ---求当前日期前一周
select dateadd(mm,-1,getdate())     ---求当前日期前一月


select dateadd(yy,-1,getdate())     ---求当前日期前一年
select dateadd(yy,-2,getdate())     ---求当前日期前两年
select dateadd(yy,-3,getdate())     ---求当前日期前三年


SELECT convert(varchar,DATEADD(day, -1, cast('2014-10-16' as datetime)),112)

SELECT convert(varchar,DATEADD(day, -1, cast('2014-10-16' as datetime)),23)


select DateDiff(dd,cast('2014-10-16' as datetime),cast('2014-10-15' as datetime))

SELECT DATEADD(day, -1, getdate())

SELECT DATEADD(day, -1, cast('2014-10-16' as datetime))
datediff结合cast函数求出两日期之间的年差、月差、日差_第1张图片

你可能感兴趣的:(SQL)