Birthday SQL Function - sql

I am trying to create an expression named "Birthday" to use in a given filter that reads "Birthday is equal to #today" to return any birthdays in a given day. The only way to get the birthday is based on the Date of Birth field. Obviously the DOB YEAR differs from the current YEAR. I need help creating a SQL expression that recognizes somebody's birthday without taking the year into consideration.

This should do the trick:
DECLARE #THEDAY INT = 1
DECLARE #THEMONTH INT = 1
IF OBJECT_ID('BDAY_TABLE') IS NOT NULL
DROP TABLE BDAY_TABLE
CREATE TABLE BDAY_TABLE (ID INT,
BDAY DATETIME)
INSERT INTO BDAY_TABLE (ID, BDAY)
VALUES (1, '1/1/2000'),
(2, '2/10/2000'),
(2, '1/1/2010'),
(2, '10/30/2005')
SELECT * FROM BDAY_TABLE
SELECT * FROM BDAY_TABLE
WHERE DATEPART(DAY, BDAY) = #THEDAY
AND DATEPART(MONTH, BDAY) = #THEMONTH
results:
1 2000-01-01 00:00:00.000
2 2010-01-01 00:00:00.000
Note the use of DATEPART, that way we don't really care about the year.

You can use the DATEADD and YEAR functions to calculate the Birthday in a given year. Supposed that you have a variable #TODAY that holds the current date, you could use this as your WHERE clause to get only those people that celebrate their birthday today:
WHERE DATEADD(year, YEAR(#TODAY)-YEAR(DOB), DOB) = #TODAY
This will also overcome the leap-year problem that way that such people will be returned on March 1st in a non-leapyear.
But most likely you want to get informed on birthdays let's say 2 weeks ahead, so I suggest to indeed calculate the next Birthday in order to be able to use whatever criteria on that, for example:
DECLARE #TODAY datetime = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0);
SELECT LastName, FirstName, Email, DOB, Birthday, YEAR(Birthday)-YEAR(DOB) AS becoming
FROM People
CROSS APPLY ( VALUES (
CASE WHEN DATEADD(year, YEAR(#TODAY)-YEAR(DOB), DOB) < #TODAY
THEN DATEADD(year, 1 + YEAR(#TODAY)-YEAR(DOB), DOB)
ELSE DATEADD(year, YEAR(#TODAY)-YEAR(DOB), DOB)
END)
) bd (Birthday)
WHERE Birthday <= DATEADD(day, 14, #TODAY);

Related

Trying to calculate a date of birth

I have a table with 300 people in it and need to calculate their ages as of 04/01/2017 based on their DOBs. I know I am missing populating my #dob variable and can't figure it out. Here is what I have:
Declare #dob datetime
Declare #cutoff datetime
set #cutoff = '2017-04-01'
Select dob, FLOOR((CAST (#cutoff AS INTEGER) - CAST(#dob AS INTEGER)) / 365.25) AS Age,
FROM [PGADCP].[dcp].[person] p
This is complicated in SQL Server. What you need to do is subtract three months and one day and use datediff() as of '2016-12-31'.
That is:
select p.dob,
datediff(year, dateadd(month, -3, dob), '2016-12-31') as AgeAtCutoff
The reason is that datediff() counts year boundaries. Presumably, you don't want to add a year of age to someone born in December.
Another approach is the approximate approach:
select datediff(day, dob, '2017-04-01') / 365.25
This works pretty well for most things.
Rather than trying to compute the number of full years manually, use DATEDIFF function with year parameter:
SELECT
p.dob
, DATEDIFF(year, p.dob, #cutoff) AS AgeAsOfCutoff
FROM [PGADCP].[dcp].[person] p
Note that you do not need #dob variable at all, because the value of dob comes from the corresponding column in [person] table.
Here's one method to calculate the age:
DECLARE #cutoff datetime = '20170401';
SELECT
(CAST(CONVERT(char(8), #cutoff, 112) AS int) -
CAST(CONVERT(char(8), dob, 112) AS int)) /10000 AS Age
FROM [PGADCP].[dcp].[person];
This should give you their exact age from the cut-off
declare #asat date = '2017-01-01'
SELECT (DATEDIFF(HOUR, date_of_birth, #asat) / 8766) AS [Age]
from myPeople
Test
declare #asat date = '2017-11-17'
declare #mydbo date = '1981-11-18'
SELECT (DATEDIFF(HOUR, #mydbo, #asat) / 8766) AS [Age]
That's simple math. Subtract the birth year from the year 2017. Then check whether birth month/day is before 01/04, because in this case you must subtract a year, as that year's birthday was not reached yet.
select
dob,
2017 - year(dob) - case when '01/04' > convert(char(5), dob, 1) then 1 else 0 end as age
from pgadcp.dcp.person;

SQL to calculate age based on year and month

I want to get a list of names who are turning 14 year old and 11 months. Can someone to help me fix the SQL below? Thanks!
SELECT NAME
FROM TABLE
WHERE
(cast(datediff(DAY, DOB, getDate() -1) /(365.23076923074) as int)>=14
AND cast(datediff(MONTH, DOB, getDate() -1) % (12) as int)>=11)
OR
(cast(datediff(DAY, DOB, getDate() -1) /(365.23076923074) as int)<=15
AND cast(datediff(MONTH, DOB, getDate() -1) % (12) as int)<=0)
These people turn 14, 11 months, today.
select name
from table
where DOB = cast(dateadd(mm,-11,dateadd(yy,-14,getdate())) as date)
--this is to eliminate the time on getdate
I am not sure if you need <= or just <. Please make changes accordingly.
select NAME from table Where DOB < = DATEADD(MONTH, -179, GETDATE())
Or If you are just looking for results between a span of one month then:
select NAME from table Where DOB BETWEEN DATEADD(MONTH, -179, GETDATE()) AND DATEADD(MONTH, -180, GETDATE())

How to calculate age (in years) based on Date of Birth in SQL

I want to get the 1st of the month following the date the person turns 70. How to achive this in SQL
i am calculating age using the formula
DECLARE #Date_of_birth DATETIME = '1915-10-02 00:00:00.000'
DECLARE #AGE INT
SELECT #AGE = FLOOR((CAST (GETDATE() AS INTEGER) - CAST(#Date_of_birth AS INTEGER)) / 365.25)
IF(#AGE > 70)
How to find the first of the month following the date ??
IF (#AGE >80)
You can use datediff to calculate their age, and then date add to find their 70th birthday. To find the first of the month afterwards, you can use the Month and Year functions.
create table #people (name varchar(30), birthdate date)
insert into #people
values ('Bob', '07/08/1976'), ('Tasha','05/30/1996'),('April','04/01/1971')
--This will give you everyone's age
select DATEDIFF(YY,birthdate,GETDATE()) as age
from #people
--This will give you the first month following the date that they turn 70
select Name, DATEADD(yy,70,birthdate) as [70thBday], convert(varchar,month(dateadd(m,1,DATEADD(yy,70,birthdate)))) + '/01/' + convert(varchar,YEAR(dateadd(m,1,DATEADD(yy,70,birthdate))))
from #people
declare #dob datetime = '1954-06-08'
declare #age int = 70
select DATEADD(m, DATEDIFF(m, -1, DATEADD(yy, #age, #dob)), 0)
you can use dateadd function like this
select dateadd(d,1 - datepart(d,dateadd(m,1,dateadd(yy,70",,#Date_of_birth)),dateadd(m,1,dateadd(yy,70",,#Date_of_birth)))
You can break down this query like this
declare #BD_70 date = dateadd(yy,70",,#Date_of_birth)
declare #Nxt_Month_70 = dateadd(m,1,#BD_70 date)
declare #First_Of_Month_70 = dateadd(d,1 - datepart(d,#Nxt_Month_70),#Nxt_Month_70)
Basically, you add 70 year, find next month and replace the day to the first of the month.
Here is how you would calculate age base on current date.
select case
when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date)
then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate()))
else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate()))
end as MemberAge
go
This is how I calculate the age:
SELECT DATEDIFF(yy, [DateOfBirth], GETDATE()) + (CASE WHEN DATEPART(MONTH, GETDATE()) - DATEPART(MONTH, [DateOfBirth]) < 0 THEN -1 ELSE 0 END ) AS Age FROM [User]
You can try TIMESTAMPDIFF function.(Support MySQL, Apache Doris)
Example:
select TIMESTAMPDIFF(year,'1996-09-09',now()) as age;

Function that calculates age in SQL

I'm looking for a way to calculate the age of a person. I'm making a function for it that I can use later as well as this time.
Function should take a ID-Number which are 11 characters in sweden format which is 560404-1234.
And return the age of the person just the years. So far I've thought of making a subtraction with example: Getdate year 2014 - 19+IDNr datepart yy to get the sum.
Still stuck though.
Cheers
I am guessing that the Swedish id number format is YYMMDD followed by something else.
If so, then the following should work to get the date:
select cast('19'+left(idn, 6) as date)
Then the age would be:
select datediff(year, cast('19'+left(idn, 6) as date), getdate())
What do the Swedes do about people born in the last 14 or so years? What happens to those who are over 100 years old?
If "dean" is correct, then you can try following code,
Create FUNCTION [dbo].[udfn_Get_Age_In_Years]
(
#ID_Number varchar(50)
)
Returns int
--select dbo.[udfn_Get_Age_In_Years]('560404-1234')
as
begin
Declare #retVal int = 0
select #retVal = Datediff(mm,col_dob,GETDATE()) / 12 from myTable
where ID_Number = #ID_Number
Return #retVal
end
This is my answer it's similar to the above ones but this is how I've solved it after a while.
ALTER function [dbo].[Function_AGE]
(
#IdNr varchar(13)
)
returns int
as
begin
Declare #Calc int
Set #IdNr = (Select Case when LEN(#IdNr) > 11
then cast(left(#IdNr, 8) as date)
else cast('19'+left(#IdNr,6) as date) end)
set #Calc = (select datediff(year,#IdNr,getdate()))
Return #Calc
end
Here is how you would calculate age of a person, given their age and current date
select case
when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date)
then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate()))
else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate()))
end as MemberAge
go
To Compare a birthdate to #Date, the DATEDIFF gets you close. Subtract off of that if the month and day haven't occurred yet in the year.
CASE WHEN DATEPART(mm,birthdate) > DATEPART(mm,#Date)
THEN DATEDIFF(YEAR, birthdate AS DATE), #Date) - 1
WHEN DATEPART(mm,birthdate) = DATEPART(mm,#Date)
AND DATEPART(dd,birthdate) > DATEPART(dd,#Date)
THEN DATEDIFF(YEAR, birthdate AS DATE), #Date) - 1
ELSE DATEDIFF(YEAR, CAST(birthdate AS DATE), #Date)
END AS Age

SQL Select Upcoming Birthdays

I'm trying to write a stored procedure to select employees who have birthdays that are upcoming.
SELECT * FROM Employees WHERE Birthday > #Today AND Birthday < #Today + #NumDays
This will not work because the birth year is part of Birthday, so if my birthday was '09-18-1983' that will not fall between '09-18-2008' and '09-25-2008'.
Is there a way to ignore the year portion of date fields and just compare month/days?
This will be run every monday morning to alert managers of birthdays upcoming, so it possibly will span new years.
Here is the working solution that I ended up creating, thanks Kogus.
SELECT * FROM Employees
WHERE Cast(DATEDIFF(dd, birthdt, getDate()) / 365.25 as int)
- Cast(DATEDIFF(dd, birthdt, futureDate) / 365.25 as int)
<> 0
Note: I've edited this to fix what I believe was a significant bug. The currently posted version works for me.
This should work after you modify the field and table names to correspond to your database.
SELECT
BRTHDATE AS BIRTHDAY
,FLOOR(DATEDIFF(dd,EMP.BRTHDATE,GETDATE()) / 365.25) AS AGE_NOW
,FLOOR(DATEDIFF(dd,EMP.BRTHDATE,GETDATE()+7) / 365.25) AS AGE_ONE_WEEK_FROM_NOW
FROM
"Database name".dbo.EMPLOYEES EMP
WHERE 1 = (FLOOR(DATEDIFF(dd,EMP.BRTHDATE,GETDATE()+7) / 365.25))
-
(FLOOR(DATEDIFF(dd,EMP.BRTHDATE,GETDATE()) / 365.25))
Basically, it gets the # of days from their birthday to now, and divides that by 365 (to avoid rounding issues that come up when you convert directly to years).
Then it gets the # of days from their birthday to a week from now, and divides that by 365 to get their age a week from now.
If their birthday is within a week, then the difference between those two values will be 1. So it returns all of those records.
In case someone is still looking for a solution in MySQL (slightly different commands), here's the query:
SELECT
name,birthday,
FLOOR(DATEDIFF(DATE(NOW()),birthday) / 365.25) AS age_now,
FLOOR(DATEDIFF(DATE_ADD(DATE(NOW()),INTERVAL 30 DAY),birthday) / 365.25) AS age_future
FROM user
WHERE 1 = (FLOOR(DATEDIFF(DATE_ADD(DATE(NOW()),INTERVAL 30 DAY),birthday) / 365.25)) - (FLOOR(DATEDIFF(DATE(NOW()),birthday) / 365.25))
ORDER BY MONTH(birthday),DAY(birthday)
Best use of datediff and dateadd. No rounding, no approximates, no 29th of february bug, nothing but date functions
ageOfThePerson = DATEDIFF(yyyy,dateOfBirth, GETDATE())
dateOfNextBirthday = DATEADD(yyyy,ageOfThePerson + 1, dateOfBirth)
daysBeforeBirthday = DATEDIFF(d,GETDATE(), dateofNextBirthday)
Thanks to #Gustavo Cardoso, new definition for the age of the person
ageOfThePerson = FLOOR(DATEDIFF(d,dateOfBirth, GETDATE())/365.25)
Liked the approach of #strelc, but his sql was a bit off. Here's an updated version that works well and is simple to use:
SELECT * FROM User
WHERE (DATEDIFF(dd, getdate(), DATEADD(yyyy,
DATEDIFF(yyyy, birthdate, getdate()) + 1, birthdate)) + 1) % 366 <= <number of days>
edit 10/2017: add single day to end
You could use the DAYOFYEAR function but be careful when you want to look for January birthdays in December. I think you'll be fine as long as the date range you're looking for doesn't span the New Year.
Sorry didn't see the requirement to neutralize the year.
select * from Employees
where DATEADD (year, DatePart(year, getdate()) - DatePart(year, Birthday), Birthday)
between convert(datetime, getdate(), 101)
and convert(datetime, DateAdd(day, 5, getdate()), 101)
This should work.
My guess is using "365.25" soon or later would be fail.
So I test the working solution using "365.25"
And It don't return the same numbers of rows for every case.
Here an example:
http://sqlfiddle.com/#!3/94c3ce/7
test with year 2016 and 2116 and you will see the difference. I only can post one link but change de /7 by /8 to see both queries. (/10 and /11 for the first answer)
So, I suggest this another query, where the point is determinate next birthday from a starting date and then compare if it is in my range of interest.
SELECT * FROM Employees
WHERE
CASE WHEN (DATEADD(yyyy,DATEDIFF(yyyy, birthdt, #fromDate),birthdt) < #fromDate )
THEN DATEADD(yyyy,DATEDIFF(yyyy, birthdt, #fromDate)+1,birthdt)
ELSE DATEADD(yyyy,DATEDIFF(yyyy, birthdt, #fromDate),birthdt) END
BETWEEN #fromDate AND #toDate
This is solution for MS SQL Server:
It returns employees with birthdays in 30 days.
SELECT * FROM rojstni_dnevi
WHERE (DATEDIFF (dd,
getdate(),
DATEADD ( yyyy,
DATEDIFF(yyyy, rDan, getdate()),
rDan)
nex )
+365) % 365 < 30
I found the solution for this. This may save someone's precious time.
select EmployeeID,DOB,dates.date from emp_tb_eob_employeepersonal
cross join dbo.GetDays(Getdate(),Getdate()+7) as dates where weekofmonthnumber>0
and month(dates.date)=month(DOB) and day(dates.date)=day(DOB)
GO
/****** Object: UserDefinedFunction [dbo].[GetDays] Script Date: 11/30/2011 13:19:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--SELECT [dbo].[GetDays] ('02/01/2011','02/28/2011')
ALTER FUNCTION [dbo].[GetDays](#startDate datetime, #endDate datetime)
RETURNS #retValue TABLE
(Days int ,Date datetime, WeekOfMonthNumber int, WeekOfMonthDescription varchar(10), DayName varchar(10))
AS
BEGIN
DECLARE #nextDay int
DECLARE #nextDate datetime
DECLARE #WeekOfMonthNum int
DECLARE #WeekOfMonthDes varchar(10)
DECLARE #DayName varchar(10)
SELECT #nextDate = #startDate, #WeekOfMonthNum = DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH,0,#startDate),0),#startDate) + 1,
#WeekOfMonthDes = CASE #WeekOfMonthNum
WHEN '1' THEN 'First'
WHEN '2' THEN 'Second'
WHEN '3' THEN 'Third'
WHEN '4' THEN 'Fourth'
WHEN '5' THEN 'Fifth'
WHEN '6' THEN 'Sixth'
END,
#DayName
= DATENAME(weekday, #startDate)
SET #nextDay=1
WHILE #nextDate <= #endDate
BEGIN
INSERT INTO #retValue values (#nextDay,#nextDate, #WeekOfMonthNum, #WeekOfMonthDes, #DayName)
SELECT #nextDay=#nextDay + 1
SELECT #nextDate = DATEADD(day,1,#nextDate),
#WeekOfMonthNum
= DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH,0, #nextDate),0), #nextDate) + 1,
#WeekOfMonthDes
= CASE #WeekOfMonthNum
WHEN '1' THEN 'First'
WHEN '2' THEN 'Second'
WHEN '3' THEN 'Third'
WHEN '4' THEN 'Fourth'
WHEN '5' THEN 'Fifth'
WHEN '6' THEN 'Sixth'
END,
#DayName
= DATENAME(weekday, #nextDate)
CONTINUE
END
WHILE(#nextDay <=31)
BEGIN
INSERT INTO #retValue values (#nextDay,#nextDate, 0, '', '')
SELECT #nextDay=#nextDay + 1
END
RETURN
END
Make a cross join with the dates and check for the comparison of month and dates.
In less than a month:
SELECT * FROM people WHERE MOD( DATEDIFF( CURDATE( ) , `date_birth`) /30, 12 ) <1 and (((month(`date_birth`)) = (month(curdate())) and (day(`date_birth`)) > (day (curdate() ))) or ((month(`date_birth`)) > (month(curdate())) and (day(`date_birth`)) < (day (curdate() ))))
You could use DATE_FORMAT to extract the day and month parts of the birthday dates.
EDIT: sorry i didn't see that he wasn't using MySQL.
Assuming this is T-SQL, use DATEPART to compare the month and date separately.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
Alternatively, subtract January 1st of the current year from everyone's birthday, and then compare using the year 1900 (or whatever your epoch year is).
Most of these solutions are close, but you have to remember a few extra scenarios. When working with birthdays and a sliding scale, you must be able to handle the transition into the next month.
For example Stephens example works great for birthdays up until the last 4 days of the month. Then you have a logic fault as the valid dates if today was the 29th would be :29, 30, AND then 1, 2, 3 of the NEXT month, so you have to condition for that as well.
An alternative would be to parse the date from the birthday field, and sub in the current year, then do a standard range comparison.
Another thought: Add their age in whole years to their birthday (or one more if their Birthday hasn't happened yet and then compare as you do above. Use DATEPART and DATEADD to do this.
http://msdn.microsoft.com/en-us/library/ms186819.aspx
The edge case of a range spanning the year would have to have special code.
Bonus tip: consider using BETWEEN...AND instead of repeating the Birthday operand.
This should work...
DECLARE #endDate DATETIME
DECLARE #today DATETIME
SELECT #endDate = getDate()+6, #today = getDate()
SELECT * FROM Employees
WHERE
(DATEPART (month, birthday) >= DATEPART (month, #today)
AND DATEPART (day, birthday) >= DATEPART (day, #today))
AND
(DATEPART (month, birthday) < DATEPART (month, #endDate)
AND DATEPART (day, birthday) < DATEPART (day, #endDate))
I faced the same problem with my college project a few years ago. I responded (in a rather weasel way) by splitting the year and the date(MM:DD) in two separate columns. And before that, my project mate was simply getting all the dates and programatically going through them. We changed that because it was too inefficient - not that my solution was any more elegant either. Also, its probably not possible to do in a database that has been in use for a while by multiple apps.
Give this a try:
SELECT * FROM Employees
WHERE DATEADD(yyyy, DATEPART(yyyy, #Today)-DATEPART(yyyy, Birthday), Birthday) > #Today
AND DATEADD(yyyy, DATEPART(yyyy, #Today)-DATEPART(yyyy, Birthday), Birthday) < DATEADD(dd, #NumDays, #Today)
Nuts! A good solution between when I started thinking about this and when I came back to answer. :)
I came up with:
select (365 + datediff(d,getdate(),cast(cast(datepart(yy,getdate()) as varchar(4)) + '-' + cast(datepart(m,birthdt) as varchar(2)) + '-' + cast(datepart(d,birthdt) as varchar(2)) as datetime))) % 365
from employees
where (365 + datediff(d,getdate(),cast(cast(datepart(yy,getdate()) as varchar(4)) + '-' + cast(datepart(m,birthdt) as varchar(2)) + '-' + cast(datepart(d,birthdt) as varchar(2)) as datetime))) % 365 < #NumDays
You don't need to cast getdate() as a datetime, right?
This is a combination of a couple of the answers that was tested. This will find the next brithday after a certain date and the age they will be. Also the numdays will limit the range you are looking 7 days = week etc.
SELECT DISTINCT FLOOR(DATEDIFF(dd,Birthday, #BeginDate) / 365.25) + 1 age,
DATEADD(yyyy, FLOOR(DATEDIFF(dd,Birthday, #BeginDate) / 365.25) + 1, Birthday) nextbirthday, birthday
FROM table
WHERE DATEADD(yyyy, FLOOR(DATEDIFF(dd,Birthday, #BeginDate) / 365.25) + 1, Birthday) > #BeginDate
AND DATEADD(yyyy, FLOOR(DATEDIFF(dd,Birthday, #BeginDate) / 365.25) + 1, Birthday) < DATEADD(dd, #NumDays, #BeginDate)
order by nextbirthday
The best way to achieve the same is
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SELECT Member.* from vwMember AS Member
WHERE (DATEADD(YEAR, (DATEPART(YEAR, #StartDate) -
DATEPART(YEAR, Member.dBirthDay)), Member.dBirthDay)
BETWEEN #StartDate AND #EndDate)
Upcoming Birthday for the Employee - Sqlserver
DECLARE #sam TABLE
(
EmployeeIDs int,
dob datetime
)
INSERT INTO #sam (dob, EmployeeIDs)
SELECT DOBirth, EmployeeID FROM Employee
SELECT *
FROM
(
SELECT *, bd_this_year = DATEADD(YEAR, DATEPART(YEAR, GETDATE()) - DATEPART(YEAR, dob), dob)
FROM #sam s
) d
WHERE d.bd_this_year > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND d.bd_this_year <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 3)
I used this for MySQL, probably not the most efficient way to query but simple enough to implement.
select * from `schema`.`table` where date_format(birthday,'%m%d') >= date_format(now(),'%m%d') and date_format(birthday,'%m%d') < date_format(DATE_ADD(NOW(), INTERVAL 5 DAY),'%m%d');
i believe this ticket has been closed ages ago but for the benefit of getting the correct sql query please have a look.
SELECT Employee_Name, DATE_OF_BIRTH
FROM Hr_table
WHERE
/**
fetching the original birth_date and replacing the birth year to the current but have to deduct 7 days to adjust jan 1-7 birthdate.
**/
datediff(d,getdate(),DATEADD(year,datediff(year,DATEADD(d,-7,hr.DATE_OF_BIRTH),getdate()),hr.date_of_birth)) between 0 and 7
-- current date looks ahead to 7 days for upcoming modified year birth date.
order by
-- sort by no of days before the birthday
datediff(d,getdate(),DATEADD(year,datediff(year,DATEADD(d,-7,hr.DATE_OF_BIRTH),getdate()),hr.date_of_birth))
Better and easy solution:
select * from users with(nolock)
where date_of_birth is not null
and
(
DATEDIFF(dd,
DATEADD(yy, -(YEAR(GETDATE())-1900),GETDATE()), --Today
DATEADD(yy, -(YEAR(date_of_birth)-1901),date_of_birth)
) % 365
) = 30
I hope this helps u in some way....
select Employeename,DOB
from Employeemaster
where day(Dob)>day(getdate()) and month(DOB)>=month(getDate())
This solution also takes care for birthdays in the next year and the ordering:
(dob = day of birth; bty = birthday this year; nbd = next birthday)
with rs (bty) as (
SELECT DATEADD(Year, DATEPART(Year, GETDATE()) - DATEPART(Year, dob), dob) as bty FROM Employees
),
rs2 (nbd) as (
select case when bty < getdate() then DATEADD(yyyy, 1, bty) else bty end as nbd from rs
)
select nbd, DATEDIFF(d, getdate(), nbd) as diff from rs2 where DATEDIFF(d, getdate(), nbd) < 14 order by diff
This version, which avoids comparison of the dates, could be faster:
with rs (dob, bty) as (
SELECT dob, DATEADD(Year, DATEPART(Year, GETDATE()) - DATEPART(Year, DOB), DOB) as bty FROM employee
),
rs2 (dob, nbd) as (
select dob, DATEADD(yyyy, FLOOR(ABS((-1*(SIGN(DATEDIFF(d, getdate(), bty))))+0.1)), bty) as nbd from rs
),
rs3 (dob, diff) as (
select dob, datediff(d, getdate(), nbd) as diff from rs2
)
select dob, diff from rs3 where diff < 14 order by diff
If the range covers the 29 of February in the next year, then use:
with rs (dob, ydiff) as (
select dob, DATEPART(Year, GETDATE()) - DATEPART(Year, DOB) as ydiff from Employee
),
rs2 (dob, bty, ydiff) as (
select dob, DATEADD(Year, ydiff, dob) as bty, ydiff from rs
),
rs3 (dob, nbd) as (
select dob, DATEADD(yyyy, FLOOR(ABS((-1*(SIGN(DATEDIFF(d, getdate(), bty))))+0.1)) + ydiff, dob) as nbd from rs2
),
rs4 (dob, ddiff, nbd) as (
select dob, datediff(d, getdate(), nbd) as diff, nbd from rs3
)
select dob, nbd, ddiff from rs4 where ddiff < 68 order by ddiff
You can also use DATEPART:
-- To find out Today's Birthday
DECLARE #today DATETIME
SELECT #today = getdate()
SELECT *
FROM SMIS_Registration
WHERE (DATEPART (month, DOB) >= DATEPART (month, #today)
AND DATEPART (day, DOB) = DATEPART (day, #today))
Below query will return all next birthday of employee, it is shortest query.
SELECT
Employee.DOB,
DATEADD(
mm,
(
(
(
(
DATEPART(yyyy, getdate())-DATEPART(yyyy, Employee.DOB )
)
+
(
1-
(
((DATEPART(mm, Employee.DOB)*100)+DATEPART(dd, Employee.DOB))
/
((DATEPART(mm, getdate())*100) + DATEPART(dd, getdate()))
)
)
)
*12
)
),
Employee.DOB
) NextDOB
FROM
Employee
ORDER BY
NextDOB ;
Above query will cover all next month excluding current date.
Solution for SQLite3:
SELECT
*,
strftime('%j', birthday) - strftime('%j', 'now') AS days_remaining
FROM
person
WHERE :n_days >= CASE
WHEN days_remaining >= 0 THEN days_remaining
ELSE days_remaining + strftime('%j', strftime('%Y-12-31', 'now'))
END
;
The solutions dividing by 325.25 to get the age, or bringing the birthdate to the current year etc. didn't work for me.
What this does is computes the delta of the two daysOfTheYear (1-366). If the birthday didn't happen yet this year, you automatically get the correct number of remaining days, which you can compare to.
If the birthday already happened, remaining_days will be negative, and you can get the correct number of remaining days by still adding the total amount of days in the current year. This also correctly handles leap years, since in that case the extra day will be added as well (By using dayOfYear(Dec 31.))
select BirthDate,Name from Employees
order by Case
WHEN convert(nvarchar(5),BirthDate,101) > convert(nvarchar(5),GETDATE(),101) then 2
WHEN convert(nvarchar(5),BirthDate,101) < convert(nvarchar(5),GETDATE(),101) then 3
WHEN convert(nvarchar(5),BirthDate,101) = convert(nvarchar(5),GETDATE(),101) then 1 else 4 end ,convert(nvarchar(2),BirthDate,101),convert(nvarchar(2),BirthDate,105)