--建立测试环境
Create table [TEST]
(IDInt,
BirthdayVarchar(10))
--插入数据
Insert [TEST] Values(1,'1981/12/26')
Insert [TEST] Values(2,'1977/02/26')
Insert [TEST] Values(3,'1978/07/05')
Insert [TEST] Values(4,'1979/08/01')
Insert [TEST] Values(5,'1980/07/27')
Insert [TEST] Values(6,'1980/07/25')
GO
--测试
Select * from TEST
Where DateAdd(year,Year(Getdate())-Year(Birthday),Birthday)
Between Convert(Varchar(10),Getdate(),120)
And Convert(Varchar(10),Getdate()+30,120)
--删除测试环境
Drop Table TEST
--结果
/*
IDBirthday
41979/08/01
51980/07/27
61980/07/25
*/
Create table [TEST]
(IDInt,
BirthdayVarchar(10))
--插入数据
Insert [TEST] Values(1,'1981/12/26')
Insert [TEST] Values(2,'1977/02/26')
Insert [TEST] Values(3,'1978/07/05')
Insert [TEST] Values(4,'1979/08/01')
Insert [TEST] Values(5,'1980/07/27')
Insert [TEST] Values(6,'1980/07/25')
GO
--测试
Select * from TEST
Where DateAdd(year,Year(Getdate())-Year(Birthday),Birthday)
Between Convert(Varchar(10),Getdate(),120)
And Convert(Varchar(10),Getdate()+30,120)
--删除测试环境
Drop Table TEST
--结果
/*
IDBirthday
41979/08/01
51980/07/27
61980/07/25
*/
本文介绍了一个使用SQL进行日期运算的示例,通过测试环境搭建、数据插入及查询语句展示了如何筛选出生日在当前日期前后一定范围内的记录。
1463

被折叠的 条评论
为什么被折叠?



