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?
To return the current date, datetime, time, or timestamp, you can use the CURRENT_[date part] function 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.
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);
Yes, there are equivalent functions in both, standardSQL and legacySQL.
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
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
In Google's Big Query, the function to use is: format_datetime('%A',yourdate) instead of format_date as posted in 2017.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With