[Oracle|오라클] 한글 초성 검색 함수

직원명을 초성으로 검색할수 있도록 요청이 들어와서
급하게 만들었다.

직원수가 아주많은곳에서는 where절에 사용하면 안됨...

예) fn_korinitialkeyword('홍길동')
return : ㅎㄱㄷ

select *
from 테이블
where fn_korinitialkeyword(직원명컬럼) like '%ㅎㄱㄷ%'

 function fn_korinitialkeyword( str in varchar2) return varchar2
is
   returnStr varchar2(100); 
   cnt number := 0
   tmpStr varchar2(10);
begin 
      if str is null then
      return '';
      end if; 
     
      cnt := length(str); 

      for i in 1 .. cnt
      loop
        tmpStr := substr(str,i,1);
                
      returnStr := returnStr ||
      case when tmpStr < 'ㄱ' then substr(tmpStr, 1, 1)
            when ascii('ㄱ') <= ascii(tmpStr) and ascii(tmpStr) <= ascii('ㅎ') then chr(ascii(tmpStr))
            when tmpStr < '나' then 'ㄱ'
            when tmpStr < '다' then 'ㄴ'
            when tmpStr < '라' then 'ㄷ'
            when tmpStr < '마' then 'ㄹ'
            when tmpStr < '바' then 'ㅁ'
            when tmpStr < '사' then 'ㅂ'
            when tmpStr < '아' then 'ㅅ'
            when tmpStr < '자' then 'ㅇ'
            when tmpStr < '차' then 'ㅈ'
            when tmpStr < '카' then 'ㅊ'
            when tmpStr < '타' then 'ㅋ'
            when tmpStr < '파' then 'ㅌ'
            when tmpStr < '하' then 'ㅍ'
            else 'ㅎ'
      end
      end loop;
     
      return returnStr;
end;

 

댓글

Designed by JB FACTORY