0

This query returns no rows :

SELECT * FROM t WHERE t.s ~ '^(N_|B_).*'

while this one does

SELECT * FROM t WHERE t.s like 'N_%' or t.s like 'B_%';

Why are they not equivalent ?

1 Answer 1

1

An underscore character has special meaning in LIKE, for the documentation:

An underscore (_) in pattern stands for (matches) any single character; a percent sign (%) matches any sequence of zero or more characters. (...) To match a literal underscore or percent sign without matching other characters, the respective character in pattern must be preceded by the escape character.

So these queries are equivalent:

SELECT * FROM t WHERE t.s ~ '^(N_|B_).*';
SELECT * FROM t WHERE t.s like 'N\_%' or t.s like 'B\_%';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.