Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Day name in Google BigQuery

How to get the name of the day from date in Google BigQuery.

I can achieve this by below query

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 'Thusday' WHEN DAYOFWEEK(CURRENT_DATE())=6 THEN 'Friday'
             WHEN DAYOFWEEK(CURRENT_DATE())=7 THEN 'Saturday' END as [DOW]

If there is any default function available to get name of the day?

like image 561
selva kumar Avatar asked Jul 09 '15 13:07

selva kumar


People also ask

How do you get the day in BigQuery?

To return the current date, datetime, time, or timestamp, you can use the CURRENT_[date part] function in BigQuery.

How do you add days to a date in BigQuery?

SELECT DATE_ADD(DATE "2021-01-01", INTERVAL 2 DAY) AS two_days_later; The DATE_ADD BigQuery gives the following result. +——————–+, in the YYYY-MM-DD format. In the same way, if we use DATE_ADD BigQuery to add 25 days to 7th September 2021, we get 2nd October 2021.

How do you get the first day of year in BigQuery?

Use DATETIME_TRUNC function SELECT DATE_TRUNC('2021-05-20', month); Result: 2021-05-01. This function can also be used to get the first day of a quarter or a year, etc. SELECT DATE_TRUNC('2021-05-20', year);


2 Answers

Yes, there are equivalent functions in both, standardSQL and legacySQL.

Standard SQL

You can use the FORMAT_DATE() function where you can use any date format you'd like. Link to Docs

Example:

#standardSQL
SELECT
  CURRENT_DATE() AS date,
  FORMAT_DATE('%A', CURRENT_DATE()) AS dow

Legacy SQL

There is the STRFTIME_UTC_USEC() function. It requires converting your timestamp to USEC first though. Link to docs

Example:

#legacySQL
SELECT
  CURRENT_DATE() AS date,
  STRFTIME_UTC_USEC(TIMESTAMP_TO_USEC(TIMESTAMP(CURRENT_DATE())), '%A') AS dow
like image 189
Tomas Avatar answered Sep 28 '22 15:09

Tomas


In Google's Big Query, the function to use is: format_datetime('%A',yourdate) instead of format_date as posted in 2017.

like image 41
Meder Mamutov Avatar answered Sep 28 '22 15:09

Meder Mamutov