Solved

SQL Query question

Posted on 2013-01-17
2
265 Views
Last Modified: 2013-01-17
Good day. I am trying to use the following to find the Age as of a specific date (11/1/12). It doesn't seem to work for all. On some, the age does match what it should be but on some it adds an extra year.

DOB field contains dates as "1997-06-06 00:00:00.000"

The query is CAST(Round((DateDiff(d,id.DOB,'2012-11-01')/365.25),0)  as INT) as AGE

For instance, I have one record where the DOB field is "1995-05-01 00:00:00.000" and the Age comes out as 18 but should be 17.

Any idea how I can get it so that it provides the proper age as of the date requested?
0
Comment
Question by:mig1980
2 Comments
 
LVL 39

Accepted Solution

by:
appari earned 250 total points
Comment Utility
that was because of round function you used.
use floor function as follows
CAST(floor((DateDiff(d,id.DOB,'2012-11-01')/365.25))  as INT) as AGE
0
 

Author Comment

by:mig1980
Comment Utility
Perfect. Thank you very much!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now