SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Rounding down a number
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bhussey
Starting Member

5 Posts

Posted - 09/24/2012 :  15:00:33  Show Profile  Reply with Quote
I am writing a query that will show an employee age to determine if they are eligible for a certain benefit. If the employee is not 21 years of age at the time this query is run they are not eligible.

select round((sysdate-birthdate)/365.25),2
from server.table
where employee = '1234'

Result = 20.78370525642000659112226531802164930157

I'd actually like to see the Result = 20

Thank you

singularity
Posting Yak Master

152 Posts

Posted - 09/24/2012 :  15:02:22  Show Profile  Reply with Quote
You can use the floor function to round down:

http://msdn.microsoft.com/en-us/library/ms178531.aspx
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 09/24/2012 :  15:55:29  Show Profile  Reply with Quote
Can you just CAST the result to an INTEGER?
Go to Top of Page

bhussey
Starting Member

5 Posts

Posted - 09/24/2012 :  18:01:40  Show Profile  Reply with Quote
The floor function works, thanks for the help
Go to Top of Page

bhussey
Starting Member

5 Posts

Posted - 09/24/2012 :  18:05:35  Show Profile  Reply with Quote
I do have a additional question for this. Now I need to identify if this result is 21 or greater. I tried
(sysdate-birthdate)/365.25) >= '21' but no results
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 09/24/2012 :  18:58:52  Show Profile  Reply with Quote
A couple of things:

1. The FLOOR function will return the same data type as the expression being floored. What that data type is in your case, I'm not sure. It might even be a float but, I suspect it's Numeric. If it is a Float/Real, that might cause comparison issues.
2. You have wrapped the value-literal in single quotes ('21') that denotes a string. I'd have to look up the casting precedence to know for sure what SQL is doing, but that is probably your issue. You should not rely on implicit casting by SQL, rather you should explicitly cast the to the data type you want it to be.
CAST(((sysdate-birthdate)/365.25) AS INT) >= 21
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000