Saturday, March 9, 2013

Calculating Age using MySQL

We want to  Calculating Age Current date from Date of Birth in MySQL.Following Given many option find out  age.



SELECT NOW()  - Returns the current date and time
Output- 2013-03-09 14:16:44
SELECT CURDATE()  - Returns the current date
Output- 2013-03-09


1. SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT('1981-10-25', '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT('1981-10-25', '00-%m-%d'))
AS age
output- 31

2. SELECT YEAR(DATE_SUB(NOW(), INTERVAL TO_DAYS('1981-10-25') DAY)) AS age
output- 31

3. SELECT ROUND(DATEDIFF(CURDATE(),'1981-10-25')/365) AS age 
output- 31

MySQL Date Functions

DATE_SUB() - The DATE_SUB() function subtracts a specified time interval from a date.
CURTIME() - Returns the current time
DATE() - Extracts the date part of a date or date/time expression
EXTRACT() -Returns a single part of a date/time
DATE_ADD()- Adds a specified time interval to a date
DATEDIFF()- Returns the number of days between two dates
DATE_FORMAT()- Displays date/time data in different formats
TO_DAYS() - Return the date argument converted to days

More function see it

No comments:

Post a Comment