Created
March 26, 2012 02:09
-
-
Save truedat101/2202294 to your computer and use it in GitHub Desktop.
Some SQL dating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simple query to convert dob (date) into number) */ | |
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dob)), '%Y')+0 AS age FROM my_tbl | |
/* Simple query to add WHERE clause into above computed field, get all aged above 50 */ | |
select age from (SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dob)), '%Y')+0 AS age FROM my_tbl) bar where age > 50; | |
/* Simple query to add WHERE clause into above computed field, get all aged above 25 and less than 70 */ | |
select age from (SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dob)), '%Y')+0 AS age FROM my_tbl) bar | |
WHERE age > 25 | |
AND age < 70 | |
/* Simple select a temp table with age and some id */ | |
SELECT age, b.anID | |
FROM ( | |
SELECT DATE_FORMAT( FROM_DAYS( TO_DAYS( NOW( ) ) - TO_DAYS( dob ) ) , '%Y' ) +0 AS age | |
FROM my_tbl | |
) bar, my_tbl b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment