Adding Days To Date in SQL
- by Coding Noob
I am trying to get data from my Database of those who have upcoming birth days in next few days(declared earlier)
it's working fine for days but this query will not work if i add 24 days to current date cause than it will need change in month.
i wonder how can i do it
declare @date int=10,
@month int=0
select * from STUDENT_INFO where DATEPART(DD,STDNT_DOB) between
DATEPART(DD,GETDATE()) and DATEPART(DD,DATEADD(DD,@date,GETDATE()))
and
DATEPART(MM,STDNT_DOB) = DATEPART(MM,DATEADD(MM,@month,GETDATE()))
This query works fine but it only checks date between 8 & 18
but if i use it like this
declare @date int=30,
@month int=0
select * from STUDENT_INFO where DATEPART(DD,STDNT_DOB) between
DATEPART(DD,GETDATE()) and DATEPART(DD,DATEADD(DD,@date,GETDATE()))
and
DATEPART(MM,STDNT_DOB) = DATEPART(MM,DATEADD(MM,@month,GETDATE()))
it will return nothing since it require addition in month as well
If I Use it like this
declare @date int=40,
@month int=0
select * from STUDENT_INFO where DATEPART(DD,STDNT_DOB) between
DATEPART(DD,GETDATE()) and DATEADD(DD,@date,GETDATE())
and
DATEPART(MM,STDNT_DOB) = DATEPART(MM,DATEADD(MM,@month,GETDATE()))
than it will return results till the last of this month but will not show till 18/12 which was required