CASE WHEN E.SECOND_LAST_NAME = '?' THEN '' ELSE E.SECOND_LAST_NAME END AS SECOND_LAST_NAME,

E.SUFFIX,

CASE WHEN S.RESTRICT = 'C' THEN E.GENDER ELSE '***' END AS GENDER

FROM databasename.EMPLOYEE E

LEFT OUTER JOIN

(SELECT COALESCE(MAX(RESTRICT_CODE), 'X') AS RESTRICT

FROM databasename.EDW_SECURITY

WHERE USER_ID = CASE WHEN POSITION('_' IN USER) = 3 THEN SUBSTR(USER,4) ELSE USER END

AND 1 = CASE WHEN (POSITION('_' IN USER) = 3 AND SUBSTR(USER,1,2) = FUNCTION_CODE) OR POSITION('_' IN USER) = 0 THEN 1 ELSE 0 END) AS S ON 1=1

If the user were not in the EDW_SECURITY table, the column (in this case GENDER) would be masked with '***'. We have other processes around this type of security but just thought I would provide you with this flavor.