Database

Notion Formulas: contains() vs test() Functions

💭 Formulas Explained

The contains() and test() function in Notion conduct similar tests on database properties. Both functions allow the user to check for strings in another property. For example, to check for the string “Hello” in a string property named “Name”, call contains(prop("Name"), "Hello") or test(prop("Name"), "Hello"). Both codes will return a true boolean. What exactly is the difference between these two functions then?

 

Limitations of Notion’s contains() function

contains() syntax → contains(text, match text)

Looking at the example above, a formula named “2022?” is calling on the property named “Name” to check for the string “2022.” 2/3 of the database titles have the string “2022.” This code (contains(prop("Name"), "2022")) is therefore valid. Let’s look at the next example.

If I wanted to call on the number property named “Year” instead, the formula will break. This is because the property being called in contains() must be a string type like text, select, multi-select, url, person, relation, files & media, email, status, or a string rollup.

 

Notion’s test() Function and Compatibility

test() syntax() → test(number, text) or test(text, text) or test(boolean, text)

The problem that occurred with testing a number property above can be fixed with the test() function (test(prop("Year"), "2022")). It is also compatible with numbers and booleans.

If you are familiar will regular expressions, the test() function is compatible with these symbols. This is not the case for contains(). For example, above is a test to check for any instance of the number 5 or 6. This is displayed in a matrix like this: test(prop("Name"), "[56]"). There are 2/3 page titles in the database that contain at least one 5 or 6. Using the same matrix method will not work in the contains() function. Learn more about regular expressions.

 

In Summary …

  • The contains() function is limited to calling on string properties and it is not compatible with regular expressions.

  • The test() function is compatible with string, number, and boolean properties. Unlike the contains() function it is compatible with regular expressions.

 

Further Reading