Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

NextBirthDayDate

DECLARE @date_of_birth date = '2004-02-29'
DECLARE @todays_date date = '2014-03-01'

SELECT
      DATEADD(YEAR,
            -- Number of whole years between date of birth and today's date plus 1
            CASE                                
                  -- Month of date of birth greater than month of today's date
                  WHEN DATEPART(MONTH, @date_of_birth) > DATEPART(MONTH, @todays_date)
                  THEN DATEDIFF(YEAR, @date_of_birth, @todays_date)                              

                  -- Month of date of birth same as month of today's date and day of date of birth greater than day of today's date
                  WHEN DATEPART(MONTH, @date_of_birth) = DATEPART(MONTH, @todays_date) AND DATEPART(DAY, @date_of_birth) > DATEPART(DAY, @todays_date)
                  THEN
                        CASE
                              -- Date of birth is on a leap day and this year is not a leap year
                              WHEN DATEPART(MONTH, @date_of_birth) = 2 AND DATEPART(DAY, @date_of_birth) = 29 
                                          AND NOT (DATEPART(YEAR, @todays_date) % 400 = 0 OR (DATEPART(YEAR, @todays_date) % 100 <> 0 AND DATEPART(YEAR, @todays_date) % 4 = 0))
                              THEN DATEDIFF(YEAR, @date_of_birth, @todays_date) + 1
                              -- Else
                              ELSE DATEDIFF(YEAR, @date_of_birth, @todays_date)
                        END

                  -- Else
                  ELSE DATEDIFF(YEAR, @date_of_birth, @todays_date) + 1
            END,
            @date_of_birth)
Source by oliverpower.wordpress.com #
 
PREVIOUS NEXT
Tagged: #NextBirthDayDate
ADD COMMENT
Topic
Name
4+9 =