메뉴 건너뛰기

tnt_db

Oracle 특정문자까지의 왼쪽 문자열 반환 function

운영자 2002.10.18 11:26 조회 수 : 4044 추천:42

CREATE OR REPLACE FUNCTION LEFT  
(String CHAR, Pole CHAR) RETURN CHAR IS
  string_length INTEGER;
  length INTEGER;
  temp VARCHAR2(2);
  leftstring VARCHAR2(100);
  column_count INTEGER;
BEGIN

  SELECT LENGTHB(String) INTO string_length FROM DUAL;


  column_count := 0;  

  IF String is null THEN
      RETURN NULL;
    END IF;

  
  FOR i IN 1..32760 LOOP

    column_count := column_count + 1;

    SELECT SUBSTR(String,column_count, 1) INTO temp FROM DUAL;    

    IF temp = Pole THEN
      SELECT SUBSTR(String, 1, column_count - 1) INTO leftstring FROM DUAL;
      RETURN leftstring;  
    END IF;

    
    IF i = string_length THEN
      EXIT;
    END IF;
    
  END LOOP;

  RETURN NULL;

END LEFT;
위로