SELECT A.EMPLID , A.COUNTRY , A.NATIONAL_ID_TYPE , CASE WHEN A.NATIONAL_ID_TYPE = 'CPF' THEN %Substring(A.NATIONAL_ID , 1 , 7) ELSE A.NATIONAL_ID END , CASE WHEN A.NATIONAL_ID_TYPE = 'CPF' THEN %Substring(A.NATIONAL_ID , 8 , 4) WHEN B.CTPS_SERIES_BRA IS NULL THEN ' ' ELSE B.CTPS_SERIES_BRA END FROM PS_PERS_NID A LEFT OUTER JOIN PS_PERSON_BRA B ON B.EMPLID = A.EMPLID WHERE A.COUNTRY = 'BRA' AND (A.NATIONAL_ID_TYPE <> 'CPF' OR (A.NATIONAL_ID_TYPE = 'CPF' AND NOT EXISTS ( SELECT 'X' FROM PS_PERS_NID A1 WHERE A1.EMPLID = A.EMPLID AND A1.COUNTRY = A.COUNTRY AND A1.NATIONAL_ID_TYPE = 'CTPS')))
|