PURPOSE
-------
다음은 특정위치의 문자열이 한글인지 영문인지 즉, 문자유형을 판별하는
방법에 대해 간단한 예와 함께 소개한다.
Explanation
-----------
PL/SQL을 사용할 때 문자열의 특정 위치에 있는 문자가 ASCII인지, 한글의 첫번째
바이트인지, 또는 한글의 두번째 바이트인지를 확인해야 할 경우가 있습니다.
이런 경우, 문자 코드 값이 특정 값(예를 들어, KSC5601의 경우에는 0xA1)보다 큰지
작은지를 비교하여 판단하는 루틴을 사용하면 DB CharacterSet이 다른 경우에는
전혀 사용할 수 없습니다. 아래의 PL/SQL 함수는 Oralce WebServer의 NLS 문제를
해결하기 위해 만든 것입니다. 절대값과 비교하는 방식을 사용하지 않았으므로
DB CharacterSet에 관계없이 사용할 수 있습니다.
Example
-------
/* determine character type from cbuf of specified position bytepos */
/* bytepos is zero-based */
/* return : 0 if ascii, 1 if 1st byte of DBCS, 2 if 2nd byte of DBCS */
function nls_getchartype (cbuf in varchar2, bytepos in integer) return
integer
is
loc integer:=0;
ret integer:=0;
begin
if cbuf is NULL then
return 0;
end if;
while (loc <= bytepos)
loop
if (ret = 1 ) then
ret := 2;
elsif (length(substrb(cbuf, loc+1, 2)) = 1) then
ret := 1;
else
ret := 0;
end if;
loc := loc + 1;
end loop;
return ret;
end;
위가 않되는 경우 아래의 function을 실행하십시요.
create or replace function nls (cbuf in varchar2, bytepos in integer) return
integer
is
loc integer:=1;
ret integer:=0;
begin
if cbuf is NULL then
return 0;
end if;
while (loc <= bytepos)
loop
if (ret = 1 ) then
ret := 2;
elsif (length(substrb(cbuf, loc, 2)) = 1) then
ret := 1;
else
ret := 0;
end if;
loc := loc + 1;
end loop;
return ret;
end;
**실행 **
SQL>variable a number;
SQL> exec :a := nls(`t하t`,1);
PL/SQL procedure successfully completed.
SQL> print a
A
----------
0
SQL> exec :a:=nls(`t하t`,2);
PL/SQL procedure successfully completed.
SQL> print a
A
----------
1
SQL> exec :a := nls(`t하t`,3);
PL/SQL procedure successfully completed.
SQL> print a
A
----------
2
-------
다음은 특정위치의 문자열이 한글인지 영문인지 즉, 문자유형을 판별하는
방법에 대해 간단한 예와 함께 소개한다.
Explanation
-----------
PL/SQL을 사용할 때 문자열의 특정 위치에 있는 문자가 ASCII인지, 한글의 첫번째
바이트인지, 또는 한글의 두번째 바이트인지를 확인해야 할 경우가 있습니다.
이런 경우, 문자 코드 값이 특정 값(예를 들어, KSC5601의 경우에는 0xA1)보다 큰지
작은지를 비교하여 판단하는 루틴을 사용하면 DB CharacterSet이 다른 경우에는
전혀 사용할 수 없습니다. 아래의 PL/SQL 함수는 Oralce WebServer의 NLS 문제를
해결하기 위해 만든 것입니다. 절대값과 비교하는 방식을 사용하지 않았으므로
DB CharacterSet에 관계없이 사용할 수 있습니다.
Example
-------
/* determine character type from cbuf of specified position bytepos */
/* bytepos is zero-based */
/* return : 0 if ascii, 1 if 1st byte of DBCS, 2 if 2nd byte of DBCS */
function nls_getchartype (cbuf in varchar2, bytepos in integer) return
integer
is
loc integer:=0;
ret integer:=0;
begin
if cbuf is NULL then
return 0;
end if;
while (loc <= bytepos)
loop
if (ret = 1 ) then
ret := 2;
elsif (length(substrb(cbuf, loc+1, 2)) = 1) then
ret := 1;
else
ret := 0;
end if;
loc := loc + 1;
end loop;
return ret;
end;
위가 않되는 경우 아래의 function을 실행하십시요.
create or replace function nls (cbuf in varchar2, bytepos in integer) return
integer
is
loc integer:=1;
ret integer:=0;
begin
if cbuf is NULL then
return 0;
end if;
while (loc <= bytepos)
loop
if (ret = 1 ) then
ret := 2;
elsif (length(substrb(cbuf, loc, 2)) = 1) then
ret := 1;
else
ret := 0;
end if;
loc := loc + 1;
end loop;
return ret;
end;
**실행 **
SQL>variable a number;
SQL> exec :a := nls(`t하t`,1);
PL/SQL procedure successfully completed.
SQL> print a
A
----------
0
SQL> exec :a:=nls(`t하t`,2);
PL/SQL procedure successfully completed.
SQL> print a
A
----------
1
SQL> exec :a := nls(`t하t`,3);
PL/SQL procedure successfully completed.
SQL> print a
A
----------
2
'프로그래밍 > DB' 카테고리의 다른 글
[펌]TABLE의 RECORD 건수를 확인하는 방법 (0) | 2008.01.09 |
---|---|
[펌]DATE의 기간을 계산하는 방법 (0) | 2008.01.09 |
[펌]XML 파일을 PLSQL을 이용해서 CLOB에 저장하는 예제 (0) | 2008.01.09 |
[펌] mysql start & shutdown (0) | 2007.11.27 |
[펌] 계정 생성 및 권한부여 (0) | 2007.11.27 |