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

时间:2016-11-24 13:20:52

标签: sql timestamp google-bigquery dayofweek

我正在使用BigQuery 标准SQL。

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

e.g。将今天的时间戳(2016-11-24 00:00:00)转换为字符串:'Thursday'

谢谢:)

3 个答案:

答案 0 :(得分:5)

以下是BigQuery Standard 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     

答案 1 :(得分:0)

您必须使用DAYOFWEEK()然后使用CASE语句返回星期几。 DAYOFWEEK()以1(星期日)和7(星期六)之间的整数形式返回星期几。 Date and time functions

e.g。

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

答案 2 :(得分:0)

在 BigQuery 中,您可以使用:

C++11

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