生日提醒,用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)
select '20020901 ' union all
select '20010917 ' union all
select '19820701 ' union all
select '19730912 ' union all
select '19761206 ' union all
select '19680918 '
go
declare @Date datetime
set @Date = GetDate()
Select * From Staff
Where
DateDiff(Day,DateAdd(Year,DateDiff(year,Staff_Time,@Date),Staff_Time),@Date) between 0 and 3
Drop table Staff
------------------------------------------------------
id Staff_Time
----------- ------------------------------------------------------
2 2001-09-17 00:00:00.000
6 1968-09-18 00:00:00.000
(所影响的行数为 2 行)