Click here to Skip to main content
15,924,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used the below code to check whether date of birth is in upcoming 30 days,it is working fine but if current date is in december month & birthday is in january month it is not working

SQL
if datepart(dayofyear, @DateofBirth) between datepart(dayofyear,getdate()) and datepart(dayofyear, dateadd(day,30,getdate()))


how to fix the problem?

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 22-Jun-11 21:45pm
v4

Well, duh. DayOfYear means number of days in the year. So, datepart(dayofyear, dateadd(day,30,getdate())) will be between 1 and 31 if your birthday is in Dec. You should try thinking about your code, or running sections of it, if you can't work out what it does.

If getdate() returns a date in Dec, you need to add 365 to the second value, because otherwise you're asking if (350) is between (345) and (5), or similar.
 
Share this answer
 
hello

select ID,EmpName,substring((convert(varchar,DoB,107)),0,7) as DoB,ImagePath from tblBirthDay where datepart(dayofyear,DOB) between datepart(dayofyear,getdate()) and datepart(dayofyear, dateadd(day,7,getdate()))



try this this is working for me. this is for with in week from today b'day you can change for 30 days.


thanks
sanjeev
 
Share this answer
 
Comments
OriginalGriff 23-Jun-11 4:01am    
Reason for my vote of one: This will do nothing to help the OP. It is the same code with a shorter duration, and will exhibit teh same error as the OP is asking about. For example, check it with a birthday on 28th December. What value does it return for datepart(dayofyear, dateadd(day,7,getdate()) : answer "4".
Christian Graus 23-Jun-11 4:07am    
But thanks for posting something that's totally wrong, after I posted the correct answer.
[no name] 23-Jun-11 4:07am    
try it first then vote.. this is working for me.
Sergey Alexandrovich Kryukov 23-Jun-11 4:20am    
"Working for me" is wrong argument! Do you think a test prove anything? My 1.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900