Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement "like" in BigQuery?

I am trying to run a simple query making a restriction of like % in BigQuery, but LIKE is not in their syntax, so how can it be implemented?

like image 853
Lengoman Avatar asked Oct 11 '12 01:10

Lengoman


People also ask

How do I write a like statement in SQL?

The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Is like case sensitive in BigQuery?

Case Sensitivity - Unlike most RDBMS, BigQuery is case sensitive, not only for string comparison, but for object names as well.

Is BigQuery like SQL?

BigQuery supports the Google Standard SQL dialect, but a legacy SQL dialect is also available. If you are new to BigQuery, you should use Google Standard SQL as it supports the broadest range of functionality. For example, features such as DDL and DML statements are only supported using Google Standard SQL.

How do you write not equal in a large query?

NOT EQUAL TO (!=) and EXISTS... EQUAL TO Giving Different Results.


4 Answers

You can use the REGEXP_MATCH function (see the query reference page): REGEXP_MATCH('str', 'reg_exp')

Instead of using the % syntax used by LIKE, you should use regular expressions (detailed syntax definition here)

like image 162
Jordan Tigani Avatar answered Oct 16 '22 06:10

Jordan Tigani


REGEXP_MATCH is great if you know how to use it, but for those who aren't sure there won't be any commonly used special characters such as '.','$' or '?' in the lookup string, you can use LEFT('str', numeric_expr) or RIGHT('str', numeric_expr). ie if you had a list of names and wanted to return all those that are LIKE 'sa%' you'd use:

select name from list where LEFT(name,2)='sa'; (with 2 being the length of 'sa')

Additionally, if you wanted to say where one column's values are LIKE another's, you could swap out the 2 for LENGTH(column_with_lookup_strings) and ='sa' for =column_with_lookup_strings, leaving it looking something like this:

select name from list where LEFT(name,LENGTH(column_with_lookup_strings))= column_with_lookup_strings;

https://cloud.google.com/bigquery/query-reference

like image 38
tomb Avatar answered Oct 16 '22 08:10

tomb


LIKE is officially supported in BigQuery Standard SQL - https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#comparison_operators

And I think it also works in Legacy SQL!

like image 30
Mikhail Berlyant Avatar answered Oct 16 '22 08:10

Mikhail Berlyant


REGEXP_MATCH returns true if str matches the regular expression. For string matching without regular expressions, use CONTAINS instead of REGEXP_MATCH.

https://developers.google.com/bigquery/docs/query-reference#stringfunctions

like image 28
Jipo Avatar answered Oct 16 '22 06:10

Jipo