Search
 
SCRIPT & CODE EXAMPLE
 

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)
Comment

PREVIOUS NEXT
Code Example
Sql :: postgres row expiration 
Sql :: price-colour 
Sql :: sütun güncelleme SQL 
Sql :: nuget sqllite-net-pcl 
Sql :: SQL Injection Using Multiple Statement 
Sql :: suhaib 
Sql :: sql gather statistics to increase performance 
Sql :: get who is hired in month in sql 
Sql :: mysql let join 
Sql :: postgresql using reserved word as column name 
Sql :: why we have to set the password for my sql server 
Sql :: phpmyadmin mysql conflict 
Sql :: delete hangfire retries list 
Sql :: alling a function from PL/SQL in a select statement in ORACLE 
Sql :: Filter on observations that are null SQL 
Sql :: function sum(text) does not exist postgres 
Sql :: creating directory /var/lib/postgresql/data ... initdb: error: could not create directory "/var/lib/postgres/data": Permission denied 
Sql :: min varias colunas spark sql 
Sql :: joins vs includes 
Sql :: azure sql server check foreign key 
Sql :: Laravel SQLSTATE[HY093] with array query 
Sql :: c# execute transact sql 
Sql :: mysql does sentance contain word 
Sql :: Uncaught PDOException: SQLSTATE[HY000] [1698] 
Sql :: mysql exception output 
Sql :: connect google bigquery connect using sqirrel 
Sql :: c# sql transaction multiple connections 
Sql :: flask sqlalchemy single table inheritance 
Sql :: postgresql exploit 
Sql :: mod function example 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =