ALTER FUNCTION "DBA"."get_skoop_kood"( in a_kood char(50) default '' )
RETURNS INTEGER
BEGIN
  declare ll_skoop integer;
  set a_kood=Left(Trim(a_kood),50);
  if a_kood is null or length(a_kood)=0 then
    set ll_skoop=0
  else
    if length(a_kood)<17 then
      select first skoobid.skoop into ll_skoop from skoobid where skoobid.kood=a_kood
    end if;
    if ll_skoop is null then
      select first skoobid.skoop into ll_skoop from skoobid where skoobid.nimi=a_kood
    end if;    
  end if;
  if ll_skoop is null then
    set ll_skoop=0
  end if;
  return ll_skoop;
END