Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query question

Posted on 2013-01-17
2
Medium Priority
?
272 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 1000 total points
ID: 38789981
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
ID: 38790039
Perfect. Thank you very much!!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

926 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