看完你的问题,你有多种可能,希望对你有帮助
代码
- 使用 like 运算符
create or replace function F_CHECK_STRING (P_TEXT in VARCHAR2,
P_WORD in VARCHAR2)
return NUMBER IS
BEGIN
IF (LOWER(P_TEXT) LIKE '% ' || LOWER(P_WORD) || ' %' ) THEN
RETURN 1;
ELSIF (LOWER(P_TEXT) LIKE LOWER(P_WORD) || ' %' ) THEN
-- Matches the beginning of a string
RETURN 1;
ELSIF (LOWER(P_TEXT) LIKE '% ' || LOWER(P_WORD) || '' ) THEN
-- Matches the end of a string
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
/
- 使用substr运算符,请看上一个答案
- 使用 regexp_like 运算符(我的建议)
create or replace function F_CHECK_STRING (P_TEXT in VARCHAR2,
P_WORD in VARCHAR2)
return NUMBER IS
BEGIN
-- \s means a whitespace character
-- ^ Matches the beginning of a string.In multiline mode, it matches the beginning of any line anywhere within the source string.
-- $ Matches the end of a string. In multiline mode, it matches the end of any line anywhere within the source string.
-- 'i' specifies case-insensitive matching
-- more https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Oracle-Regular-Expression-Support.html
IF regexp_like (P_TEXT, '(^|\s)' || P_WORD || '(\s|$)', 'i') THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
/
测试
select F_CHECK_STRING ('In English Hello Word', 'hello') as "hello",
F_CHECK_STRING ('In English Hello Word', 'IN') as "IN",
F_CHECK_STRING ('In English Hello Word', 'he') as "he",
F_CHECK_STRING ('In English Hello Word', 'WORD') as "WORD"
from dual;
HELLO IN HE WORD
----- -- -- ----
1 1 0 1
提示
可以在sql查询中使用regexp_like,无需创建自定义函数
select 1 FROM DUAL
where regexp_like ('In English Hello Word', '(^|\s)' || 'hello' || '(\s|$)', 'i')
;
select 1 FROM DUAL
where regexp_like ('In English Hello Word', '(^|\s)' || 'he' || '(\s|$)', 'i')
;
SELECT CASE WHEN regexp_like ('In English Hello Word', '(^|\s)' || 'hello' || '(\s|$)', 'i')
THEN 1
ELSE 0
END
from dual;
SELECT CASE WHEN regexp_like ('In English Hello Word', '(^|\s)' || 'he' || '(\s|$)', 'i')
THEN 1
ELSE 0
END
from dual;