

To see a good variety, let's use some of the examples presented in the RegEx freeCodeCamp Curriculum.

You can do a lot of different things with RegEx patterns. RegEx lets you match patterns by character class (like all letters, or just vowels, or all digits), between alternatives, and other really flexible options. You can use RegEx in many languages like PHP, Python, and also SQL. RegEx on its own is a powerful tool that allows for flexible pattern recognition. What about if you need more complex pattern matching? Well, for that you need to use Regular Expressions. WHERE name NOT SIMILAR TO "%u%" RegEx in SQL This is how you would write the example we used before using SIMILAR TO instead: SELECT name FROM planets You can use this operator with NOT in front to have the opposite effect. Let's take the example used with LIKE and let's use it here too. You can use the % operator for any number of characters, and the _ operator for exactly one character. The SIMILAR TO operator works in a pretty similar way to the LIKE operator, including which metacharacters are available. You can use it in addition to or in place of LIKE. nameĭepending on the SQL flavour you are using, you might also be able to use the SIMILAR TO operator. With this query you get all planets whose names don't contain the letter u, like below. The NOT LIKE operator finds all strings that do not match the pattern. With this query you would get the below names of the planets that end with "us". Where planets is a table with the data of the solar system's planets. ² (in the example, third and fourth characters are determined) Example query SELECT name FROM planets ¹ (in the example, second to last and third to last characters are determined) Strings that have a specific substring at a specific position from the beginning² Strings that have a specific substring at a specific position from the end¹ Strings that have a specific substring anywhere in the string Strings that end with a specific substring Strings that begin with a specific substring You can use these characters in a wide variety of use-cases. The operators are used like this: column_name LIKE pattern. SQL patterns use the LIKE and NOT LIKE operators and the metacharacters (characters that stand for something other than themselves) % and _.

They have a more limited syntax than RegEx, but they're more universal through the various SQL versions. SQL patterns are useful for pattern matching, instead of using literal comparisons. In this article, we'll look at how you can use the Contains String query. Being able to do complex queries can be really useful in SQL.
