I have a few lines of code in postgreSQL which I would like to use to query an Oracle database with the same tables and columns.
However the postgres code isn't working in sql developer and returns an error. The code looks like this:
SELECT * FROM file WHERE file_name IS NOT NULL
AND file_name ~ '[\u0000-\u001f]|^$|^ | | $';
And
SELECT * FROM file WHERE file_name IS NOT NULL AND
CASE WHEN file_name LIKE '\\%' THEN right(file_name, -1)
ELSE file_name END ~ '^[.]|[.]$';
I'm having trouble converting the ~ and the right(file_name, -1) parts of the code to run them in sql developer.
Any help is greatly appreciated.
答案 0 :(得分:1)
您需要使用Oracle的substr()代替Postgres的right()
而不是regexp_like()函数,而不是~
(区分大小写的正则表达式,如运算符),而将'c'
作为3d参数(意味着大小写,尽管可以省略):
SELECT * FROM file
WHERE
file_name IS NOT NULL
AND
REGEXP_LIKE(
CASE
WHEN file_name LIKE '\\%' THEN substr(file_name, 2)
ELSE file_name
END, '^[.]|[.]$', 'c');
答案 1 :(得分:1)
right()
很少使用。但是Oracle中的等效项是substr(x, 2)
,因此一种方法是:
SELECT *
FROM file
WHERE file_name IS NOT NULL AND
REGEXP_LIKE(CASE WHEN file_name LIKE '\\%' THEN substr(file_name, 2)
ELSE file_name
END), '^[.]|[.]$');
但是,我将在两个数据库中使用单个正则表达式来做到这一点:
WHERE REGEXP_LIKE(file_name, '^(\\)?[.]|[.]$');
在进行这种类型的正则表达式匹配时,使用case
没有优势。与NULL
的比较只是多余的。