select sporg_code,spbldinf.spbldinf_code from sporg left outer join spbldinf
ON sporg.sporg_code =(select (CASE WHEN len(spbldinf.spbldinf_code)= 3 THEN '0'+spbldinf.spbldinf_code
ELSE spbldinf.spbldinf_code END ) from spbldinf where sporg.sporg_code=spbldinf.spbldinf_code)
select sporg_code,spbldinf.spbldinf_code from sporg left outer join spbldinf
ON convert(varchar,sporg.sporg_code) = right('0'+convert(varchar,spbldinf.spbldinf_code),4)