-- BDATE FROM AGETYPE
-- SELECT PAT_TIMESTAMP, PAT_BDATE, PAT_AGE, PAT_AGETYPE, substring(PAT_AGETYPE, LOCATE('/', PAT_AGETYPE)+1) AS MONTHS, DATE((date_sub(PAT_TIMESTAMP, INTERVAL substring(PAT_AGETYPE, LOCATE('/', PAT_AGETYPE)+1) MONTH))) AS BDATE 
-- FROM PATIENT
-- WHERE PAT_TIMESTAMP > 0
-- AND PAT_ID > 0
-- AND PAT_AGETYPE NOT LIKE "" 
-- AND PAT_BDATE IS NULL;

UPDATE PATIENT SET PAT_BDATE = DATE((date_sub(PAT_TIMESTAMP, INTERVAL substring(PAT_AGETYPE, LOCATE('/', PAT_AGETYPE)+1) MONTH))), PAT_AGETYPE = ""
WHERE PAT_TIMESTAMP > 0
AND PAT_ID > 0
AND PAT_AGETYPE NOT LIKE "" 
AND PAT_BDATE IS NULL;

-- BDATE FROM AGE
-- SELECT PAT_TIMESTAMP, PAT_BDATE, PAT_AGE, DATE(date_sub(PAT_TIMESTAMP, INTERVAL PAT_AGE YEAR)) AS BDATE
-- FROM PATIENT
-- WHERE PAT_TIMESTAMP > 0
-- AND PAT_ID > 0
-- AND PAT_BDATE IS NULL;

UPDATE PATIENT SET PAT_BDATE = DATE(date_sub(PAT_TIMESTAMP, INTERVAL PAT_AGE YEAR))
WHERE PAT_TIMESTAMP > 0
AND PAT_ID > 0
AND PAT_BDATE IS NULL;

-- AGE FROM BDATE
-- SELECT PAT_TIMESTAMP, PAT_BDATE, PAT_AGE, PAT_AGETYPE, TIMESTAMPDIFF(YEAR, PAT_BDATE, CURDATE()) AS AGE
-- FROM PATIENT
-- WHERE PAT_TIMESTAMP > 0
-- AND PAT_ID > 0
-- AND PAT_BDATE IS NOT NULL;

UPDATE PATIENT SET PAT_AGE = TIMESTAMPDIFF(YEAR, PAT_BDATE, CURDATE())
WHERE PAT_TIMESTAMP > 0
AND PAT_ID > 0
AND PAT_BDATE IS NOT NULL;