Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP... Data Calculation

Status
Not open for further replies.

StacyStacy

Programmer
Apr 16, 2003
60
US
How do I get those employees who were 60 years old at date of hire? the date format is: 2002-10-16 00:00:00.000

Here's that I have and it is returning everyone:

SELECT
hire_date
,birth_date
,RTRIM(last_name) AS LastName
,RTRIM(first_name)AS FirstName
,empno
FROM empper

WHERE --("empper"."birth_date") -("empper"."hire_date") < '1951'
hire_date - birth_date >= 60
ORDER BY last_name
 
It's not returning data.

SELECT
birth_date
,hire_date
,RTRIM(last_name) AS LastName
,RTRIM(first_name)AS FirstName
,empno
FROM empper

WHERE Datediff (y,hire_date , birth_date) >= 60
--("empper"."birth_date") -("empper"."hire_date") < '1951'
ORDER BY last_name
 
Sorry about that.

It returns data if I use <= 60. So this tells me that my code is still incorrect. In the return, I see someone who is 29 years old. This person should not be listed in the return. I only want those who are 60 years old or older on their hiredate based off of their birthday. Where am I going wrong?


SELECT
birth_date
,hire_date
,RTRIM(last_name) AS LastName
,RTRIM(first_name)AS FirstName
,empno
FROM empper

WHERE Datediff (y,hire_date , birth_date) <= 60
ORDER BY last_name
 
[tt] WHERE Datediff (y , birth_date,hire_date) > 60[/tt]

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Sorry sb
WHERE Datediff (yy,birth_date , hire_date) >=60
 
Thanks so much!

I'm an intermediate coder ...
Am I understanding the logic correctly?

Select everything
WHERE the difference between two dates, looking at the last 2 digit year of (birth_date) then (hire_date) is => 60?


 
Note George's answer is greater than 60, otherwise you'll be including some 59 year olds in your list.

soi là, soi carré
 
Right, because of the operator: >=, which means 'greater than or equal to'.

Thanks! :)
 
Thinking more on this, you might be better to consider the days between birth date and hire date, rather than the crossing of the year boundary:
Code:
select datediff(dd, birth_date, hire_date)/365.25 >= 60

Consider the situation of
birth date 1950-12-31 and hired 2010-01-01 = 60 years on datediff of years
and
birth date 1950-01-01 and hired 2010-12-31 = 60 years on datediff of years
Only the second is 60 at the time of hiring.





soi là, soi carré
 
How do you write the code to compare to the entire date ... month,day and year? This didn't work for me ...

WHERE Datediff (YYYYMMDD,birth_date , hire_date) >=60

thanks,
stacy
 
Alas, one can't; the DATEDIFF function takes only one measurement of time - from Years downwards. As PWise commented and as per my example above, using year counts the number of cross-overs from one calendar year to the next, such that DATEDIFF(yy,'2011-01-01','2012-12-31') is still the same as DATEDIFF(yy,'2011-12-31','2012-01-01').
Note that when SQL server is set to English, Month is MM or M and Minute is MI or N.

An alternative tack to take, which might be easier to comprehend, is to use DATEADD on the birth_date. It uses the same time parameters as DATEDIFF.
Add 60 years to the birth_date and then compare to the hire date to see whether it precedes.

Code:
WHERE DATEADD(YY,60,birth_date) <= hire_date

HTH


soi là, soi carré
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top