HiveBrain v1.2.0
Get Started
← Back to all entries
patternMajor

What is it called when you search the middle of a string instead of the beginning?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
calledtheyousearchwhatinsteadbeginningwhenstringmiddle

Problem

I am trying to polish up my vocabulary to better communicate with my fellow developers. We have several places in the site where we are debating if we should search for a string from the beginning 'running%' vs anywhere in the string '%running%.

I have been calling the middle search "fuzzy" which I realize is incorrect as fuzzy means changing the form of the word "run", "runing" [sic], "runed" [sic].

What is the correct terminology for searching the beginning of a string and searching the middle of a string?

Solution

It's called an "un-anchored search pattern", and it looks like this in SQL.

foo LIKE '%bar%'


If you lack a % on either side, it is said that the search pattern anchors to the start or end of the string respectively. This lingo comes from the regex world.

foo LIKE 'bar%'


You would say, "the search pattern bar% anchored to the start of the string".

For comparison, a PCRE is anchored with ^ or $ tokens and it looks like ^bar or bar$. PCREs require explicit anchoring with tokens, whereas SQL LIKE statements are implicitly anchored and require explicit % to create an "un-anchored search pattern".

As a side note, you can index these types of expressions with trigrams using something like pg_trgm in PostgreSQL

Code Snippets

foo LIKE '%bar%'
foo LIKE 'bar%'

Context

StackExchange Database Administrators Q#187658, answer score: 24

Revisions (0)

No revisions yet.