我有以下记录
NAME BIRTHDATE
A 19/09/1990
B 25/09/1992
C 26/09/1993
且当前日期是19/09/2014 我希望从当前日期到下一个七天的生日记录是我的查询。
CREATE PROCEDURE [dbo].[Get_Birthday]
as
begin
Declare @CurrentDate date ,@NxtDate date
set @CurrentDate = GETDATE();
set @NxtDate = DATEADD(day,7,getdate())
print @CurrentDate
print @NxtDate
select DocId, DoctorName,DOA,Email from vw_DoctorDetail
where DOA between @CurrentDate and @NxtDate
end
答案 0 :(得分:2)
您可以尝试以下
CREATE PROCEDURE [dbo].[Get_Birthday]
as
beginDeclare @CurrentDate date ,@NxtDate date
set @CurrentDate = DATEADD(year,-DATEDIFF(year,'19000101',GETDATE()),GETDATE());
set @NxtDate = DATEADD(day,7,@CurrentDate)
print @CurrentDate
print @NxtDate
select DocId, DoctorName,DOA,Email from vw_DoctorDetail
WHERE DATEADD(year,-DATEDIFF(year,'19000101',DOA),DOA) between @CurrentDate and @NxtDate
答案 1 :(得分:2)
试试这个
SELECT DocId
,DoctorName
,DOA
,Email
FROM vw_DoctorDetail
WHERE (
DATEPART(DAY, DOA) BETWEEN DATEPART(DAY, GETDATE() + 7)
AND DATEPART(DAY, GETDATE())
)
AND DATEPART(MONTH, DOA) = DATEPART(MONTH, GETDATE())
答案 2 :(得分:2)
这将适用于sqlserver 2012+ 请注意,这是使用2000年,这是避免问题的闰年。
SELECT
DocId,
DoctorName,
DOA,
Email
FROM
vw_DoctorDetail
WHERE
datefromparts(2000, month(DOA), day(DOA))
between datefromparts(2000, month(@CurrentDate), day(@CurrentDate)) and
datefromparts(2000, month(@NxtDate), day(@NxtDate))