REGEXP_COUNT 在 postgres 中

REGEXP_COUNT in postgres

我们正在从 Oracle 迁移到 Postgres。

这里是 SQL,我以前从 employee_name 列中提取数据并用来报告。

但现在我不确定如何完成 regex_count 部分。 甲骨文 SQL

with A4 as 
(
select 'govinda j/INDIA_MH/9975215025' as employee_name from dual
)
select employee_name , 
TRIM(SUBSTR(upper(A4.employee_name),1,INSTR(A4.employee_name,'/',1,1)-1)) AS employee_name1,
  TRIM(SUBSTR(upper(A4.employee_name),INSTR(A4.employee_name,'/',1,1)+1,INSTR(A4.employee_name,'_',1,1)-INSTR(A4.employee_name,'/',1,1)-1)) AS Country,
  TRIM(SUBSTR(upper(A4.employee_name),INSTR(A4.employee_name,'_',1,1)+1,INSTR(A4.employee_name,'/',1,2)-INSTR(A4.employee_name,'_',1,1)-1)) AS STATE,
  CASE WHEN REGEXP_COUNT(A4.employee_name,'_')>1 THEN 'WRONG_NAME>1_'
       WHEN REGEXP_COUNT(A4.employee_name,'/')>2 THEN 'WRONG_NAME>2/'
       WHEN TRIM(SUBSTR(upper(A4.employee_name),INSTR(A4.employee_name,'/',1,1)+1,INSTR(A4.employee_name,'_',1,1)-INSTR(A4.employee_name,'/',1,1)-1))NOT IN
         ('INDIA','NEPAL') THEN 'WRONG_COUNTRY'
       ELSE 'CORRECT' END AS VALIDATION

       from A4

在 Postgres 的帮助下,我可以将其转换为以下部分。

with A4 as 
(
select 'govinda j/INDIA_MH/9975215025'::text as employee_name
)
select employee_name,
       split_part(employee_name, '/', 1) as employee_name1,
       split_part(split_part(employee_name, '/', 2), '_', 1) as country,
       split_part(split_part(employee_name, '/', 2), '_', 2) as state
from A4

验证 部分无法转换。非常感谢任何帮助,因为我们是 postgres 的新手。

您可以创建自定义函数:

create or replace function number_of_chars(text, text)
returns integer language sql immutable as $$
    select length() - length(replace(, , ''))
$$; 

使用:

with example(str) as (
values 
    ('a_b_c'),
    ('a___b'),
    ('abc')
)

select str, number_of_chars(str, '_') as count
from example

  str  | count  
-------+-------
 a_b_c |     2
 a___b |     3
 abc   |     0
(3 rows)

请注意,上述函数仅计算字符串中某个字符的出现次数,并未使用通常成本更高的正则表达式。

regexp_count() 的 Postgres 等价物可能如下所示:

create or replace function regexp_count(text, text)
returns integer language sql as $$
    select count(m)::int
    from regexp_matches(, , 'g') m
$$; 

with example(str) as (
values 
    ('a_b_c'),
    ('a___b'),
    ('abc')
)

select str, regexp_count(str, '_') as single, regexp_count(str, '__') as double
from example

  str  | single | double 
-------+--------+--------
 a_b_c |      2 |      0
 a___b |      3 |      1
 abc   |      0 |      0
(3 rows)

对于今天访问这个问题的任何人(像我一样),regexp_count 显然将按照以下方式包含在 Postgres 15 中:https://pgpedia.info/r/regexp_count.html

语法如下:

regexp_count ( string text, pattern text [, start integer [, flags text ] ] ) → integer