Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some SQL dating
/* 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
You can’t perform that action at this time.