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 > Formulas in a query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-11, 15:56
lightfju lightfju is offline
Registered User
 
Join Date: Sep 2010
Posts: 14
Question Formulas in a query

OK this may seem elementary but I cannot seem to get this.

I need a field to be generated in a query that comes from subtracting 2 different dates; Example:

Client DOB - 01/01/1900

Client's Child DOB - 01/01/2000

Result - Client was 100 when child was born.

Is it possible to just display the result of this equation?
Reply With Quote
  #2 (permalink)  
Old 12-01-11, 17:03
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Have a look at the function DATEDIFF which will give you in days the difference between two dates.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 12-01-11, 18:18
lightfju lightfju is offline
Registered User
 
Join Date: Sep 2010
Posts: 14
ok so would this go in the select statment?
Reply With Quote
  #4 (permalink)  
Old 12-02-11, 02:21
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Here is an example taken from MySQL documentation:

SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 12-02-11, 12:51
lightfju lightfju is offline
Registered User
 
Join Date: Sep 2010
Posts: 14
Thanks so much for your help!!

I ended up using:

Code:
FLOOR (DATEDIFF(o.birthdate,c.birthday)/365) AS 'Age of Client at Babys Birth'
Is this OK? I ask because FLOOR is not something that I am really familiar with.
Reply With Quote
  #6 (permalink)  
Old 12-02-11, 13:17
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
The function FLOOR simply rounds down to the nearest integer value. So for example, FLOOR(2.5) will return 2, FLOOR(-2.5) will return -3. In the above case you are using it correctly as a person who is 5.25 is considered 5 years old. One thing though is that the number of leap years which might cause some problems over time. I would suggest dividing by 365.25 to get something a little more accurate.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Tags
mysql, query

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