Oracle SQL에서 특정 문자까지 하위 문자열을 선택하는 방법은 무엇입니까?
다음과 같은 결과가있는 테이블 열이 있다고 가정합니다.
ABC_blahblahblah
DEFGH_moreblahblahblah
IJKLMNOP_moremoremoremore
해당 테이블에서이 열을 선택하는 쿼리를 작성하고 싶지만 밑줄 (_) 문자까지만 하위 문자열을 반환합니다. 예를 들면 :
ABC
DEFGH
IJKLMNOP
SUBSTRING 함수는 위치 기반이고 밑줄의 위치가 다양하기 때문에 작업에 적합하지 않은 것 같습니다.
TRIM 함수 (특히 RTRIM 함수)에 대해 생각했습니다.
SELECT RTRIM('listofchars' FROM somecolumn)
FROM sometable
그러나 특정 목록 / 문자 집합을 제거하는 것처럼 보이며 실제로는 밑줄 문자로 이어지는 문자를 쫓아 가기 때문에 어떻게 작동하는지 잘 모르겠습니다.
SUBSTR, INSTR 및 NVL (밑줄이없는 문자열의 경우) 조합을 사용하면 원하는 결과가 반환됩니다.
SELECT NVL(SUBSTR('ABC_blah', 0, INSTR('ABC_blah', '_')-1), 'ABC_blah') AS output
FROM DUAL
결과:
output
------
ABC
사용하다:
SELECT NVL(SUBSTR(t.column, 0, INSTR(t.column, '_')-1), t.column) AS output
FROM YOUR_TABLE t
참고:
추가
Oracle10g +를 사용하는 경우 REGEXP_SUBSTR을 통해 regex를 사용할 수 있습니다 .
REGEXP_SUBSTR을 사용하면 쉽게 할 수 있습니다.
사용하시기 바랍니다
REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1)
여기서 STRING_EXAMPLE 은 문자열입니다.
시험:
SELECT
REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1)
from dual
그것은 당신의 문제를 해결할 것입니다.
INSTR을 사용하여 첫 번째 밑줄의 위치를 얻은 다음 substr을 사용하여 첫 번째 문자에서 (pos-1)까지 문자열의 일부를 가져와야합니다.
1 select 'ABC_blahblahblah' test_string,
2 instr('ABC_blahblahblah','_',1,1) position_underscore,
3 substr('ABC_blahblahblah',1,instr('ABC_blahblahblah','_',1,1)-1) result
4* from dual
SQL> /
TEST_STRING POSITION_UNDERSCORE RES
---------------- ------------------ ---
ABC_blahblahblah 4 ABC
SELECT REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1) from dual
user1717270이 게시 한대로 정답입니다.
If you use INSTR
, it will give you the position for a string that assumes it contains "_" in it. What if it doesn't? Well the answer will be 0. Therefore, when you want to print the string, it will print a NULL
. Example: If you want to remove the domain from a "host.domain". In some cases you will only have the short name, i.e. "host". Most likely you would like to print "host". Well, with INSTR
it will give you a NULL
because it did not find any ".", i.e. it will print from 0 to 0. With REGEXP_SUBSTR
you will get the right answer in all cases:
SELECT REGEXP_SUBSTR('HOST.DOMAIN','[^.]+',1,1) from dual;
HOST
and
SELECT REGEXP_SUBSTR('HOST','[^.]+',1,1) from dual;
HOST
Another possibility would be the use of REGEXP_SUBSTR.
Remember this if all your Strings in the column do not have an underscore (...or else if null value will be the output):
SELECT COALESCE
(SUBSTR("STRING_COLUMN" , 0, INSTR("STRING_COLUMN", '_')-1),
"STRING_COLUMN")
AS OUTPUT FROM DUAL
To find any sub-string from large string:
string_value:=('This is String,Please search string 'Ple');
Then to find the string 'Ple'
from String_value
we can do as:
select substr(string_value,instr(string_value,'Ple'),length('Ple')) from dual;
You will find result: Ple
'code' 카테고리의 다른 글
PHP에서 동적 클래스 메서드 호출 (0) | 2020.10.23 |
---|---|
명시 적 형변환을 사용하여 파생 클래스 참조에 기본 클래스 개체를 할당 할 수 있습니까? (0) | 2020.10.23 |
Windows 10에 Windows SDK 7.1을 설치할 수 없습니다. (0) | 2020.10.23 |
React Router의 Link 컴포넌트 밑줄을 제거하는 방법은 무엇입니까? (0) | 2020.10.23 |
BibTeX에서 모든 대문자 유지 (0) | 2020.10.22 |