|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
calculate age based on dob
hi all;
I have seen posts on numerous websites to get the age from a dob column I am faced with the same problem but get this error: Server: Msg 403, Level 16, State 1, Line 1 Invalid operator for data type. Operator equals divide, type equals datetime. when I run the following query: SELECT ROUND((currentdate - DOB)/365.24,0) FROM FL1_A_Backup All I need is a simple statement that calcualtes the age from the dob column(smalldatetime) then creates a new row which it puts the data into. I have already created a CurrentDate column which has a (getdate()) value attached to it. This is nothing fancy or complicated and I just need to figure out from the year I'm not really concerned about if the person has a bday tomorrow or so on. I know I am probably doing something wrong with this query and am not shy to say that I have only been using ms sql for a couple of months (basic select statements). |
|
#2
|
||||
|
||||
|
you do not need a column for the current date --
Code:
select year(getdate())
- year(DOB)
- case when month(DOB) > month(getdate()) then 1
when month(DOB) < month(getdate()) then 0
when day(DOB) > day(DOB) then 1
else 0
end as age
from FL1_A_Backup
|
|
#3
|
|||
|
|||
|
awesome
great the procedure worked perfectly; now how would; using the alter table command I insert this into my table
|
|
#4
|
||||
|
||||
|
reiqwan, you do not want to store the result, just display it
|
|
#5
|
|||
|
|||
|
thats it
thanks for the help
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > calculate age based on dob |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|