0

There is a list of countries. I want to get the list of the countries that:

  1. consist of the letter U and A -or- S
  2. consist of the letter U and any character except S.

These structures are correct for the MS SQL but don't work for Postgres

For MS SQL Server:

SELECT 
    country
FROM world.country
WHERE country LIKE 'U[AS]%'

SELECT 
    country
FROM world.country
WHERE country LIKE 'U[S]%' 

For Postgres?

Luuk
  • 12,245
  • 5
  • 22
  • 33
  • 1
    May be this might be helpful https://stackoverflow.com/questions/24368404/regular-expression-in-postgresql-like-clause – coldy May 08 '22 at 17:09

1 Answers1

0

You can use SIMILAR TO Documentation

select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country SIMILAR TO 'U(A|S)%'

returns 'UAA' and 'USA'

You can use also POSIX regular expression with the same result.

select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country ~ '^U(A|S)'
Kadet
  • 1,344
  • 3
  • 10