BigQuery Standart SQL:从时间戳中提取星期几名称

BigQuery Standart SQL: extract day-of-week name from timestamp

我正在使用 BigQuery 标准 SQL。

我需要将时间戳转换为星期名称。

例如将今天的时间戳(2016-11-24 00:00:00)转换成字符串:'Thursday'

谢谢:)

您必须使用 DAYOFWEEK(),然后使用 CASE 语句 return 星期几。 DAYOFWEEK() returns 星期几作为 1(星期日)和 7(星期六)之间的整数。 Date and time functions

例如

SELECT 
  CASE 
    WHEN DAYOFWEEK(CURRENT_DATE()) = 1 THEN 'Sunday'
    WHEN DAYOFWEEK(CURRENT_DATE()) = 2 THEN 'Monday'
    WHEN DAYOFWEEK(CURRENT_DATE()) = 3 THEN 'Tuesday'
    WHEN DAYOFWEEK(CURRENT_DATE()) = 4 THEN 'Wednesday'
    WHEN DAYOFWEEK(CURRENT_DATE()) = 5 THEN 'Thursday'
    WHEN DAYOFWEEK(CURRENT_DATE()) = 6 THEN 'Friday'
    WHEN DAYOFWEEK(CURRENT_DATE()) = 7 THEN 'Saturday'
  END Weekday

以下适用于 BigQuery 标准 SQL

#standardSQL
SELECT 
  CURRENT_DATE() AS day,
  FORMAT_DATE('%a', CURRENT_DATE()) AS weekday_name_abbreviated,
  FORMAT_DATE('%A', CURRENT_DATE()) AS weekday_name_full

#standardSQL
SELECT 
  DATE('2016-11-24 00:00:00') AS day,
  FORMAT_DATE('%a', DATE('2016-11-24 00:00:00')) AS weekday_name_abbreviated,
  FORMAT_DATE('%A', DATE('2016-11-24 00:00:00')) AS weekday_name_full  

结果是

day         weekday_name_abbreviated    weekday_name_full    
2016-11-24  Thu                         Thursday     

在 BigQuery 中您可以使用:

SELECT

EXTRACT(DAYOFWEEK
  FROM
    {your_timestamp}) AS dayofweek,

FROM TABLE

它returns一个整数:1=星期一,7=星期日