Newbie question. I'm looking for stings beginning with letter 'R' this works:
SELECT *
FROM table
WHERE column LIKE 'R%'
this does not
SELECT *
FROM table
WHERE column LIKE '^R%'
WHY?
Also just curious, if I want it to end with R
, I use the $?
'%R$'
Pattern matching is not explained well here. If you know any other resources for a layman please share. thanks https://www.postgresql.org/docs/9.3/static/functions-matching.html
The Regular Expressions in PostgreSQL are implemented using the TILDE (~) operator and uses '. *” as a wildcard operator. As you can see in the figure above, we have used Regular Expression in PostgreSQL using the TILDE (~) operator and the wildcard '.
LIKE and ILIKE allow pattern matching within character-based column data. Their syntax is identical, but LIKE is case-sensitive, while ILIKE is case-insensitive.
PostgreSQL also accepts “escape” string constants, which are an extension to the SQL standard. An escape string constant is specified by writing the letter E (upper or lower case) just before the opening single quote, e.g., E'foo' .
PostgreSQL names are case sensitive. By default, AWS Schema Conversion Tool (AWS SCT) uses object name in lowercase for PostgreSQL. In most cases, you'll want to use AWS Database Migration Service transformations to change schema, table, and column names to lower case.
The manual is very clear LIKE
and regular expressions are two different things.
LIKE
Accordingly to the manual:
The LIKE expression returns true if the string matches the supplied pattern. (As expected, the NOT LIKE expression returns false if LIKE returns true, and vice versa. An equivalent expression is NOT (string LIKE pattern).)
So, LIKE returns true or false while matching a string.
%
is similar to *
in filesearch, it means it will match zero or any after or before.
R%
R and any character after.%R
Any character befor and RLIKE
it's used instead of =
, and more functionalities are explained in the manual.
-- Gets zero or any characters before one R
SELECT * FROM table WHERE column LIKE '%R'
-- Gets zero or any characters after one R
SELECT * FROM table WHERE column LIKE 'R%'
ILIKE
LIKE
is case sensitive, to make it case insensitive you can use ILIKE
REGULAR EXPRESION
In postgreSQL, for a regular expression match, instead of using =
you use ~
and the regular expression format is accordingly to the POSIX regular expressions standards
An example is:
-- Gets zero or any spaces numbers and text characters before one R
SELECT * FROM table WHERE column ~ '^[\s\w]*[R]{1}$'
-- Gets zero or any spaces numbers and text characters after one R
SELECT * FROM table WHERE column ~ '^[R]{1}[\s\w]*$'
In the manual there's the explanation for all the available operators for regular expressions. How to use regular expressions is other thing and it is in the POSIX regular expressions standards; that has nothing to do with PostgreSQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With