Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Any one to help me how i can get left days from the database. I mean to say we have database of user there is column name DOB. i just want to know how many days left for his birthday from current date.
Posted

Try this in SQL.
DECLARE @BirthDay smalldatetime
SET @BirthDay = '2011-08-21'

SELECT DATEDIFF(day, CURRENT_TIMESTAMP, DATEADD(year, DATEDIFF(year, @BirthDay, CURRENT_TIMESTAMP)
       + CASE WHEN DATEADD(year, DATEDIFF(year, @BirthDay, CURRENT_TIMESTAMP), @BirthDay) < CURRENT_TIMESTAMP 
       THEN 1 ELSE 0 END, @BirthDay))
 
Share this answer
 
v3
Comments
walterhevedeich 20-Jul-11 3:19am    
Right. My 5.
Toniyo Jackson 20-Jul-11 3:21am    
Thanks Walter :)
[no name] 20-Jul-11 3:25am    
Perfect +5.
Toniyo Jackson 20-Jul-11 3:26am    
Thanks Ram :)
Try
SQL
SELECT DATEDIFF(day, birthDate, SysDateTime());


For more information on DateDiff, see here[^].
 
Share this answer
 
Comments
Toniyo Jackson 20-Jul-11 3:14am    
If DOB is less than Current date, this query will not work.

See my answer :)

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