SELECT 'HOSPCODE','PID','SEQ','DATE_SERV','LABTEST','LABRESULT','D_UPDATE','LABPLACE','CID' union all
(SELECT v.pcucodeperson HOSPCODE,v.pid PID,vs.visitno SEQ
,concat(substr(v.datecheck,1,4),'',substr(v.datecheck,6,2),'',substr(v.datecheck,9,2))  DATE_SERV
,'0621201' LABTEST,v.hct LABRESULT
,concat(substr(v.dateupdate,1,4),'',substr(v.dateupdate,6,2),'',substr(v.dateupdate,9,2)
,'',substr(v.dateupdate,12,2),'',substr(v.dateupdate,15,2),'',substr(v.dateupdate,18,2)) D_UPDATE
,v.hoslab LABPLACE
,p.idcard CID
FROM visitlabblood v
LEFT JOIN visit vs on v.pid = vs.pid and v.datecheck=vs.visitdate
LEFT JOIN person p on v.pid =p.pid
WHERE v.hct is not null and vs.visitno is not null and v.dateupdate is not null
INTO OUTFILE 'C:/JHCIS_export/HCT/labfu.txt'
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\r\n');