Guest User

Untitled

a guest
Jul 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. DELIMITER $$
  2.  
  3. DROP FUNCTION IF EXISTS age $$
  4. CREATE FUNCTION age(birthdate varchar(10),visit_date varchar(10),date_created varchar(10),est int) RETURNS INT
  5. DETERMINISTIC
  6. BEGIN
  7. DECLARE n INT;
  8.  
  9. DECLARE birth_month INT;
  10. DECLARE birth_day INT;
  11.  
  12. DECLARE year_when_patient_created INT;
  13.  
  14. DECLARE cur_month INT;
  15. DECLARE cur_year INT;
  16.  
  17. set birth_month = (select MONTH(FROM_DAYS(TO_DAYS(birthdate))));
  18. set birth_day = (select DAY(FROM_DAYS(TO_DAYS(birthdate))));
  19.  
  20. set cur_month = (select MONTH(CURDATE()));
  21. set cur_year = (select YEAR(CURDATE()));
  22.  
  23. set year_when_patient_created = (select YEAR(FROM_DAYS(TO_DAYS(date_created))));
  24.  
  25. set n = (SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(visit_date)-TO_DAYS(DATE(birthdate))), '%Y')+0);
  26.  
  27. if birth_month = 7 and birth_day = 1 and est = 1 and cur_month < birth_month and year_when_patient_created = cur_year then set n=(n + 1);
  28. end if;
  29.  
  30. RETURN n;
  31. END $$
  32.  
  33. DELIMITER ;
Add Comment
Please, Sign In to add comment