'How to use LIKE in these cases? [duplicate]

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?



Solution 1:[1]

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)'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1