Adding Days To Date in SQL

Posted by Coding Noob on Stack Overflow See other posts from Stack Overflow or by Coding Noob
Published on 2013-11-08T07:48:39Z Indexed on 2013/11/08 9:54 UTC
Read the original article Hit count: 213

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about tsql