简体   繁体   English

BigQuery - 星期一 = 1 的星期几

[英]BigQuery - day of week number where Monday = 1

Is there a standard way to return "day of week number" (1-7) in ISO standard (Monday = 1) from a date in BigQuery using built in functions?是否有一种标准方法可以使用内置函数从 BigQuery 中的日期返回 ISO 标准(星期一 = 1)中的“星期几”(1-7)?

EXTRACT(DAYOFWEEK, <date>) returns a number using US format ie Sunday = 1, which doesn't work for my purpose. EXTRACT(DAYOFWEEK, <date>)返回一个使用美国格式的数字,即星期日 = 1,这对我的目的不起作用。

I can create a custom function to calculate the ISO day of week number but I'm not sure whether I'm missing something built in that I can use instead.我可以创建一个自定义 function 来计算 ISO 星期数,但我不确定我是否缺少可以使用的内置内容。

您可以使用模运算转换为 ISO 标准(星期一 = 1):

MOD(EXTRACT(DAYOFWEEK FROM <date>) + 5, 7) + 1

Below is example of such function - just one of many options you can come with以下是此类功能的示例 - 只是您可以提供的众多选项之一

#standardSQL
CREATE TEMP FUNCTION ISODAYOFWEEK(day DATE) AS ((
  SELECT IF(weekday = 0, 7, weekday) 
  FROM UNNEST([EXTRACT(DAYOFWEEK FROM day) - 1]) weekday 
));

I end up defining a custom function for better readability我最终定义了一个自定义 function 以获得更好的可读性

CREATE TEMP FUNCTION timestampToWeekday(dt timestamp) AS (CASE WHEN EXTRACT(DAYOFWEEK FROM dt)-1 = 0 THEN 7 ELSE EXTRACT(DAYOFWEEK FROM dt)-1 END);

And now I can use this function to convert any timestamp to a weekday.现在我可以使用这个 function 将任何时间戳转换为工作日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM