반응형

흠흠흠...

문자열 변환 함수 translate

뭐 쉽게 얘길하면

TRANSLATE("문자열", "찾을집합","변경집합")으로 변환가능.

중요한 것은 찾을 집합에 없는 값은 무조건 그냥 출력한다는 것이다.

ex) SELECT TRANSLATE('ABC-1234-5678', 'abc1234','def5678') FROM DUAL;

즉, 'ABC-1234-5678'에서 찾는 집합 'abc1234'에 매핑되는 값만 'def5678'로 변경된다.

위의 문자열중 매핑 안되는 값은 그냥 출력된다.

매핑 안되는 문자열값 : ABC-5678 이 값들은 찾을 집합에 존재하지 않으므로 그냥 skip하여 출력되며

찾는 집합 변경 집합 
 a
 b
 c
 1
 2 6
 3 7
 4 8

위의 표를 기준으로 볼때 a가 발견되면 d로 변경된다.

아래의 값에서 ABC-는 찾는집합과 변경집합이 없으므로 skip 되며

나머지 값들은 모두 해당 변경 집합으로 변경되어 출력된다.

결과
---------------
ABC-5678-5678

TRANSLATE (string1, search_set, replace_set)

Replaces every instance in string1 of a character from search_set with the corresponding character from replace_set. For example:

    TRANSLATE ('abcd', 'ab', '12') --> '12cd'

If the search set contains more characters than the replace set, then the "trailing" search characters that have no match in the replace set are not included in the result. For example:

    TRANSLATE ('abcdefg', 'abcd', 'zyx') --> 'zyxefg'

The letter 'd' is removed, because it appears in search_set without a corresponding entry in result_set.


TRANSLATE(text USING CHAR_CS) and TRANSLATE(text USING NCHAR_CS)

Translates character data to either the database character set (CHAR_CS) or the national character set (NCHAR_CS). The output datatype will be either VARCHAR2 or NVARCHAR2, depending on whether you are converting to the database or the national character set, respectively.

TRANSLATE...USING is an ISO standard SQL function. Starting with Oracle9i Database Release 1, you can simply assign a VARCHAR2 to an NVARCHAR2, (and vice versa), and Oracle will handle the conversion implicitly. If you want to make such a conversion explicit, you can use TO_CHAR and TO_NCHAR to convert text to database and national character sets, respectively. Oracle recommends the use of TO_CHAR and TO_NCHAR over TRANSLATE...USING, because those functions support a greater range of input datatypes.


출처 : 영어부분은 Oracle PL/SQL Programming 책에서 발췌...
반응형

+ Recent posts