If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > how to create QUERY to know the Current Age INCLUDING YEAR & MONTH?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-08, 20:45
magfiroh magfiroh is offline
Registered User
 
Join Date: Jun 2008
Posts: 3
how to create QUERY to know the Current Age INCLUDING YEAR & MONTH?

I have mysql, like this :
-------------------------
CREATE TABLE `EMPLOYEE` (
`ID` int(11) NOT NULL auto_increment,
`NAME` varchar(100) default NULL,
`BIRTHDAY` datetime default NULL
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
------------

my question is :
how to create QUERY to know the Current Age for each employee INCLUDING YEAR AND MONTH? it's look like this 40.11 (40 is YEAR and 11 is MONTH)

THANK
Reply With Quote
  #2 (permalink)  
Old 06-18-08, 23:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
1. create a testing table:
Code:
CREATE TABLE somedates
( adate DATE NOT NULL PRIMARY KEY
);
2. populate it with a comprehensive set of test dates

3. run this query:
Code:
SELECT adate
     , FLOOR(mths/12)           AS age_years
     , mths - 12*FLOOR(mths/12) AS age_months
 FROM ( SELECT adate
             , PERIOD_DIFF(
                 EXTRACT(YEAR_MONTH FROM 
                   DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH))
                ,190001) 
             - PERIOD_DIFF(
                 EXTRACT(YEAR_MONTH FROM 
                   adate)
                ,190001) 
             + CASE WHEN DAY(adate) <= DAY(CURRENT_DATE) 
                    THEN 1 ELSE 0 END
                AS mths
        FROM somedates ) AS dt
ORDER 
    BY adate
vwalah!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On