SELECT DISTINCT  '0' AS `E0`, '0' AS `E1`, '0' AS `PE0`, '0' AS `PE1`,  (SELECT IF(`cdisease`.`code506` IS NULL,'',`cdisease`.`code506`) AS `code506` FROM `cdisease` WHERE `cdisease`.`diseasecode`=`VISITDIAG506ADDRESS`.`diagcode`) AS `DISEASE`,  CONCAT(`person`.`fname`,' ',`person`.`lname`) AS `NAME`,  IF(`person`.`pid` IS NULL,'',`person`.`pid`) AS `HN`,  IF(`person`.`father` IS NULL, IF(`person`.`mother` IS NULL,'',`person`.`mother`) ,`person`.`father`) AS `NMEPAT`,  IF(`person`.`sex` IS NULL,'',`person`.`sex`) AS `SEX`,  getAgeYearNum(`person`.`birth`,`VISITDIAG506ADDRESS`.`sickdatestart`) AS `YEAR`,  getAgeMonthNum(`person`.`birth`,`VISITDIAG506ADDRESS`.`sickdatestart`) AS `MONTH`,  getAgeDayNum(`person`.`birth`,`VISITDIAG506ADDRESS`.`sickdatestart`) AS `DAY`,  IF(`person`.`marystatus` IS NULL,'',`person`.`marystatus`) AS `MARIETAL`,  (CASE WHEN `person`.`nation` IN ('099','99','') THEN 1    WHEN `person`.`nation` IN ('044','44') THEN 2   WHEN `person`.`nation` IN ('048','48') THEN 3   WHEN `person`.`nation` IN ('050','50') THEN 4    WHEN `person`.`nation` IN ('057','57') THEN 5    WHEN `person`.`nation` IN ('056','56') THEN 6    WHEN `person`.`nation` IN ('046','46') THEN 7    ELSE 8 END) AS RACE,  IF(`coccupa`.`occupacode506` IS NULL OR TRIM(`coccupa`.`occupacode506`)='','11',`coccupa`.`occupacode506`) AS `OCCUPAT`,  IF(`VISITDIAG506ADDRESS`.`hno` IS NULL,'',`VISITDIAG506ADDRESS`.`hno`) AS `ADDRESS`,  IF(LENGTH(`VISITDIAG506ADDRESS`.`mu`)=1, CONCAT(`VISITDIAG506ADDRESS`.`distcode`,`VISITDIAG506ADDRESS`.`subdistcode`,CONCAT('0',`VISITDIAG506ADDRESS`.`mu`)), CONCAT(`VISITDIAG506ADDRESS`.`distcode`,`VISITDIAG506ADDRESS`.`subdistcode`,`VISITDIAG506ADDRESS`.`mu`)) AS `ADDRCODE`,  IF(`VISITDIAG506ADDRESS`.`provcode` IS NULL,'',`VISITDIAG506ADDRESS`.`provcode`) AS `PROVINCE`,  (CASE TRIM(`VISITDIAG506ADDRESS`.`area`) WHEN '1' THEN '1' WHEN '2' THEN '3' ELSE '3' END) AS `METROPOL`,  '5' AS `HOSPITAL`,  '1' AS `TYPE`,  IF(`VISITDIAG506ADDRESS`.`status` IS NULL OR `VISITDIAG506ADDRESS`.`status`='','3',`VISITDIAG506ADDRESS`.`status`) AS `RESULT`,  (SELECT IF(`office`.`offid506` IS NULL,'',`office`.`offid506`) FROM `office` WHERE `office`.`offid`=`VISITDIAG506ADDRESS`.`pcucode`) AS `HSERV`,  IF(`personstudent`.`classroom` IS NULL,'',`personstudent`.`classroom`) AS `CLASSROOM`,  '' AS `SCHOOL`,  IF(`VISITDIAG506ADDRESS`.`sickdatestart` IS NULL,'',CONCAT(DATE_FORMAT(`VISITDIAG506ADDRESS`.`sickdatestart`,'%d/%m/'),DATE_FORMAT(`VISITDIAG506ADDRESS`.`sickdatestart`,'%Y')+543)) AS `DATESICK`,  IF(`VISITDIAG506ADDRESS`.`sickdatefind` IS NULL,'',CONCAT(DATE_FORMAT(`VISITDIAG506ADDRESS`.`sickdatefind`,'%d/%m/'),DATE_FORMAT(`VISITDIAG506ADDRESS`.`sickdatefind`,'%Y')+543)) AS `DATEDEFINE`,  IF(`VISITDIAG506ADDRESS`.`deaddate` IS NULL,'',CONCAT(DATE_FORMAT(`VISITDIAG506ADDRESS`.`deaddate`,'%d/%m/'),DATE_FORMAT(`VISITDIAG506ADDRESS`.`deaddate`,'%Y')+543)) AS `DATEDEATH`,  IF(`V`.`visitdate` IS NULL,'',CONCAT(DATE_FORMAT(`V`.`visitdate`,'%d/%m/'),DATE_FORMAT(`V`.`visitdate`,'%Y')+543)) AS `DATERECORD`,  IF(`V`.`visitdate` IS NULL,'',CONCAT(DATE_FORMAT(`V`.`visitdate`,'%d/%m/'),DATE_FORMAT(`V`.`visitdate`,'%Y')+543)) AS `DATEREACH`,  'TRUE' AS `INTIME`,  'FALSE' AS `TO_E1`,  '' AS `ORGANISM`,  '' AS `COMPLICA` FROM person  INNER JOIN `visit` `V` ON (`person`.`pcucodeperson`=`V`.`pcucodeperson` AND `person`.`pid`=`V`.`pid`)  INNER JOIN `visitdiag` `VD` ON (`V`.`pcucode`=`VD`.`pcucode` AND `V`.`visitno`=`VD`.`visitno`)  INNER JOIN `visitdiag506address` `VISITDIAG506ADDRESS` ON (`VISITDIAG506ADDRESS`.`pcucode`=`VD`.`pcucode` AND `VISITDIAG506ADDRESS`.`visitno`=`VD`.`visitno`)  INNER JOIN `cdisease` ON (`VISITDIAG506ADDRESS`.`diagcode`=`cdisease`.`diseasecode` AND TRIM(`cdisease`.`code506`)<>'')  LEFT JOIN `coccupa` ON (`person`.`occupa`=`coccupa`.`occupacode`)  LEFT JOIN `creligion` ON (`person`.`religion`=`creligion`.`religioncode`)  LEFT JOIN `personstudent` ON (`person`.`pcucodeperson`=`personstudent`.`pcucodeperson` AND `person`.`pid`=`personstudent`.`pid`)  WHERE `VD`.`conti`<>'1' AND `VISITDIAG506ADDRESS`.`diagcode` NOT LIKE 'U%'  AND cdisease.code506<>'98'

