问题
I have a query written to run on an Oracle database which uses the function REGEXP_LIKE to filter some rows from the query. The specific function call is
regexp_like(col1, '[^[:alpha:]]')
The problem is when I run the query on H2 I get the following error:
org.h2.jdbc.JdbcSQLException: Function "REGEXP_LIKE" not found
If I run the query directly on the Oracle database using the SQLDeveloper tool it returns as expected.
Any ideas what could be causing this?
回答1:
H2 doesn't have a function called regexp_like
. But you could create one using a user defined function:
create alias regexp_like as
$$ boolean regexpLike(String s, String p) { return s.matches(p); } $$;
drop table test;
create table test(id int, name varchar(2555));
insert into test values(1, 'Steven');
insert into test values(2, 'Stefan');
select * from test where regexp_like(name, '^Ste(v|ph)en$');
回答2:
See the excellent documentation.
col REGEXP '[^[:alpha:]]'
In general SQL variants either use a function or named operator.
Whether the above specific regex works I do not know. One should be able to rely on java regular expressions.
回答3:
Here is a slightly improved version of the H2 user function by Thomas Mueller which supports flags and NULL
values:
create alias regexp_like as
$$ boolean regexpLike(String s, String p, String flags) {
if(null == s) return false;
if(null != flags) { p = "(?" + flags + ")" + p; }
java.util.regex.Pattern compiled = java.util.regex.Pattern.compile(p);
return compiled.matcher(s).find();
} $$
回答4:
REGEXP_LIKE was added to h2 as of Version 1.4.193 (2016-10-31)
http://h2database.com/html/functions.html?#regexp_like
https://github.com/h2database/h2database/pull/323
来源:https://stackoverflow.com/questions/24677692/h2-not-recognising-regexp-like