在BigQuery中搜索日期

时间:2017-07-24 21:32:13

标签: sql google-bigquery

我在BigQuery中存储了一个日期字段。我想搜索匹配月份或年份或整个日期。如何做到以下最有效的方法?

# An example date looks like "2014-01-01"

# By year
SELECT * FROM index WHERE date LIKE '2014-%'

# By month
SELECT * FROM index WHERE date LIKE '%-01-%'

# By day
SELECT * FROM index WHERE date LIKE '%-\d\d-01'

# By year and month
SELECT * FROM index WHERE date LIKE '2014-01-%'

如何在BigQuery中以最高效的方式进行上述搜索?

注意,上面的字段属于DATE

1 个答案:

答案 0 :(得分:2)

用于BigQuery Standard SQL并假定日期字段存储为DATE类型

   

按年度

#standardSQL
SELECT * FROM index WHERE EXTRACT(YEAR FROM dt) = 2014  

按月计算

#standardSQL
SELECT * FROM index WHERE EXTRACT(MONTH FROM dt) = 1   

白天

#standardSQL
SELECT * FROM index WHERE EXTRACT(DAY FROM dt) = 1