Search code examples
postgresqlpostgresql-12

Get only character records from column


Table:

create table tbl_prefix
(
    col_pre varchar
);

Records:

insert into tbl_prefix values
('Mr.'),('Mrs.'),('Ms.'),('Dr.'),
('Jr.'),('Sr.'),('II'),('III'),
('IV'),('V'),('VI'),('VII'),
('VIII'),('I'),('IX'),('X'),
('Officer'),('Judge'),('Master');

Expected output:

col_pre
----------
Mr.
Mrs.
Ms.
Dr.
Jr.
Sr.
Officer
Judge
Master

Try:

select *
from tbl_prefix
where col_pre ~ '[^a-zA-Z]'

Getting:

col_pre
----------
Mr.
Mrs.
Ms.
Dr.
Jr.
Sr.

Solution

  • One approach here might be to match any prefix which is not a Roman numeral:

    SELECT *
    FROM tbl_prefix
    WHERE col_pre !~ '^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$';
    

    screen capture from demo link below

    Demo

    The regex pattern used here for Roman numerals was gratefully taken from this SO question:

    How do you match only valid roman numerals with a regular expression?