当前位置: 代码迷 >> Sql Server >> 生日提醒,用SQL如何写?多谢大家
  详细解决方案

生日提醒,用SQL如何写?多谢大家

热度:153   发布时间:2016-04-27 19:43:01.0
生日提醒,用SQL怎么写?谢谢大家
表:staff
staff_id   staff_name     staff_time
        1             刘德华             1981-5-1
        2             张飞                 1950-11-16
        3             王小五             1880-1-1
在存储过程中,
如果得到当日|当月|提前3天提醒|过生日的人员,除1880-1-1外

------解决方案--------------------
表:staff
staff_id staff_name staff_time
1 刘德华 1981-5-1
2 张飞 1950-11-16
3 王小五 1880-1-1
在存储过程中,
如果得到当日|当月|提前3天提醒|过生日的人员,除1880-1-1外

--当天过生日的
select * from month(staff_time) = month(getdate()) and day(staff_time) = day(getdate())
------解决方案--------------------
3天后生日的:

select * from staff where datepart(m,staff_time)=datepart(m,getdate()) and datepart(d,staff_time)=datepart(d,getdate())+3
几天生日的:
select * from staff where datepart(m,staff_time)=datepart(m,getdate()) and datepart(d,staff_time)=datepart(d,getdate())



------解决方案--------------------
---当日
select * from staff where month(staff_time) = month(dateadd(dd,3,getdate()))
and day(staff_time) = day(dateadd(dd,3,getdate()))
---当月
select * from staff where month(staff_time) = month(dateadd(dd,3,getdate()))

---3天内
select * from staff where month(staff_time) = month(dateadd(dd,3,getdate()))
and (day(staff_time) = day(dateadd(dd,3,getdate())) or
day(staff_time) = day(dateadd(dd,2,getdate())) or
day(staff_time) = day(dateadd(dd,1,getdate()))
)
------解决方案--------------------
3天后生日的:

select * from staff where datepart(m,staff_time)=datepart(m,getdate()) and datepart(d,staff_time)=datepart(d,getdate())+3
几天生日的:
select * from staff where datepart(m,staff_time)=datepart(m,getdate()) and datepart(d,staff_time)=datepart(d,getdate())
3天内生日的:

select * from staff where datepart(m,staff_time)=datepart(m,getdate()) and datepart(d,staff_time)> =datepart(d,getdate()) and datepart(d,staff_time) <=datepart(d,getdate())+3

------解决方案--------------------
select * from staff
where datediff(day,dateadd(year,datediff(year,staff_time,getdate()),staff_time),getdate()) between (0,3)



------解决方案--------------------
参考:http://community.csdn.net/Expert/topic/5757/5757414.xml?temp=.7071649
------解决方案--------------------
谢谢SoftwKLC(自由的飞鸟(卢成))
三天之内不带OR,用AND,应该怎么做,能不能
当前时间-生日时间= <3,我对这个不太懂.
----------------------------------------
用 <=3 ?应该还要大于0

select
*
from
staff
where datediff(dd,getdate(),
cast(rtrim(datepart(yy,getdate()))+ '- '+
rtrim(datepart(mm,staff_time))+ '- '+
rtrim(datepart(dd,staff_time)) as datetime)) between 0 And 3

------解决方案--------------------
Create table Staff(id int identity(1,1),Staff_Time Datetime)
go
insert into Staff(Staff_Time)
  相关解决方案