SELECT A.*,B.KEY_VALUE FROM DLAB_PROGRAMADATOSVPC.TMP_DBV_CREDITOS_MERCADO A LEFT JOIN E_DW_VIEWS.V_MST_ENCRIP_NRO_DOC_MDL_HISTDIA B ON A.NUM_DOC = CASE WHEN SUBSTR(NRO_DOC,1,2) = '10' AND LENGTH(NRO_DOC) = 11 THEN SUBSTR(NRO_DOC,3,8)  WHEN SUBSTR(NRO_DOC,1,2) = '20' AND LENGTH(NRO_DOC) = 11 THEN NRO_DOC ELSE 'X' END

Add a code snippet to your website: www.paste.org