0

I find PostgreSQL similar to operator works little strange. I accidentally checked for space in below query but surprised with the result.

select 'Device Reprocessing' similar to '%( )%' --return true select 'Device Reprocessing' similar to '%()%' --return true select 'DeviceReprocessing' similar to '%()%' --return true

Why 2nd and the 3rd query returns true? Is empty pattern always return true?

What I understand about SIMILAR TO operator is returns true or false depending on whether its pattern matches the given string.

1

1 Answer 1

2

You have defined a group with nothing in it, meaning anything will match. I think you will find any string matches %()%, even an empty string.

Normally you would use this grouping to list options so:

select 'DeviceReprocessing' similar to '%(Davinci|Dog)%'

Would return false since it contains neither "Davinci" nor "Dog", but this:

select 'DeviceReprocessing' similar to '%(vice|Dog)%'

would return true since it does contain at least one of the options.

Your first condition is true because the expression does contain a space.

I actually prefer the Regular Expression notation that does not require the % wildcards:

select 'DeviceReprocessing' ~ 'vice|Dog'
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.