| 
                
Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | irumsabaStarting Member
 
 
                                        1 Post | 
                                            
                                            |  Posted - 2011-07-07 : 03:59:14 
 |  
                                            | let it be an employee mgmt systemi haveEID (PK)Date of birth (datetime)date of joining (datetime)date of retirement(datetime)date of death (date time) i do not have to calculate age till today but i want to calculate age till date of retirement or date of death(if available) i want to declare a new column "age" and keep the records of every employee in itThis query is working for it<pre lang="sql">select case when not death is null thendatediff(year, birth, death )else datediff(year , birth, date_of_retirement ) end from Table_3</pre>it works ok, but i want to save the results in "age" |  |  
                                    | SwePesoPatron Saint of Lost Yaks
 
 
                                    30421 Posts |  |  
                                    | lappinPosting Yak  Master
 
 
                                    182 Posts | 
                                        
                                          |  Posted - 2011-08-05 : 04:19:25 
 |  
                                          | You can put your query into a calculated column in the table, or create a VIEW based on the source table plus have an additional column using your calculation |  
                                          |  |  |  
                                    | jcelkoEsteemed SQL Purist
 
 
                                    547 Posts | 
                                        
                                          |  Posted - 2011-08-07 : 12:22:17 
 |  
                                          | Every SQL forum or newsgroup expects that you will post DDL, sample data and clear specifications which might might include output. This is explained in the FAQ section. We can not read minds; what would we need to know to do your job for you? Tables must have keys and should have DRI, constraints, and all the basic features of a schema. You should know use ISO-8601 Standards for temporal data, avoid needless dialect, basic data modeling and use ISO-11179 Standards for data element names, which you do not know. Please tell us if you can change the DDL. T-SQL has a DATE data type now, so use it. CREATE TABLE Personnel_Lifetimes(emp_id CHAR(10) NOT NULL PRIMARY KEY, birth_date DATE NOT NULL, hire_date DATE NOT NULL,  CHECK (birth_date < hire_date), retirement_date DATE, --- null means still working     CHECK (hire_date < retirement_date), death_date DATE, --- null means still breathing     CHECK (hire_date < death_date)); >> I do not have to calculate age till today but I want to calculate age till date of retirement or date of death(if available) <<>> I want to declare a new column "age" and keep the records of every employee in it <<NO, you want to put the computation into a computed column or a VIEW. This is SQL and not punch card data processing. We do not materialize constantly changing data. CREATE VIEW Personnel_Lifetime_Computations (..)AS SELECT emp_id, birth_date, hire_date, retirement_date, death_date,       DATEDIFF(YEAR, birth_date,                 CASE WHEN CAST (CURRENT_TIMESTAMP AS DATE) < death_date                     THEN CAST (CURRENT_TIMESTAMP AS DATE)                      ELSE death_date END        AS emp_age,       DATEDIFF(YEAR, birth_date, retirement_date)        AS retirement_age,       DATEDIFF(YEAR, birth_date, death_date)        AS death_age  FROM Personnel_Lifetimes;See how the NULLs work? --CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |  
                                          |  |  |  
                                    | jaehakpStarting Member
 
 
                                    1 Post | 
                                        
                                          |  Posted - 2011-10-21 : 20:35:19 
 |  
                                          | declare @BIRTHDATE date ='1/2/2000'SELECT @BIRTHDATE,       DATEDIFF (year, @BIRTHDATE, getdate ())       - CASE            WHEN datepart (month, @BIRTHDATE) - datepart (month, getdate ()) <                    0            THEN               0            WHEN datepart (month, @BIRTHDATE) - datepart (month, getdate ()) =                    0            THEN               CASE                  WHEN datepart (day, @BIRTHDATE) - datepart (day, getdate ()) <=                          0                  THEN                     0                  ELSE                     1               END            ELSE               1         END          AS age |  
                                          |  |  |  
                                |  |  |  |  |  |