select
  a.ktktcd, a.ktktnm, a.ktktnk, a.ktzip, a.ktad1||' '||a.ktad2 ktad, a.kttel, a.ktfax, a.ktcls,
  a.stp, decode(a.stp,null,'inputReadOnly','inputReadOnlyStp') readcolor
from afm05 a
where(:nm is null or (upper(comnpack.MultiTOSingleBytesEx(a.ktktnk))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:nm))||'%' or
                         upper(comnpack.MultiTOSingleBytesEx(a.ktktnm))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:nm))||'%') )
  and (:zip is null or (upper(comnpack.MultiTOSingleBytesEx(a.ktzip))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:zip)||'%')) )
  and (:ad is null or (upper(comnpack.MultiTOSingleBytesEx(a.ktad1||a.ktad2))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:ad)||'%')) )
  and (:tel is null or (upper(comnpack.MultiTOSingleBytesEx(a.kttel))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:tel)||'%')) )
  and (:fax is null or (upper(comnpack.MultiTOSingleBytesEx(a.ktfax))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:fax)||'%')) )
  and (:cd is null or a.ktktcd like :cd||'%')
  and ( (lengthb(nvl(:ktcls,'0'))=2 and nvl(:ktcls,'0')=ktcls)
        or
        ( lengthb(nvl(:ktcls,'0'))=1 and   (nvl(:ktcls,'0') <>'2' or (nvl(:ktcls,'0') ='2' and (ktcls in ('02','04','70')))) )
      )
  and ((NVL(:stpflg, '0') = '1' ) or (NVL(:stpflg, '0') = '0' and a.stp is null))
order by a.ktktcd
