SQL笔记:dateadd函数

declare @dt datetime

select @dt=’2008-01-13′

select dateadd(year,20,@dt)

结果: 2028-01-13 00:00:00.000

 

 

 

declare @dt datetime
select @dt=’2008-01-13′
select dateadd(year,20,@dt)

结果: 2028-01-13 00:00:00.000

 指定日期过生日的做法:

declare @t table(ID int,Name varchar(10),Birthday datetime)
insert @t select 1,’a’,’1901-01-22′
union all select 2,’b’,’1980-12-02′
union all select 3,’c’,’1982-6-02′
union all select 4,’d’,’1980-12-02′
union all select 5,’e’,’1932-06-16′

declare @dt1 datetime,@dt2 datetime
select @dt1=’2003-05-01′,@dt2=’2003-07-02′
select * from @t
where dateadd(year,datediff(year,Birthday,@dt1),Birthday)
 between @dt1 and @dt2